Rotate environment variables (e.g., API keys) with cooldown intervals
Project description
Env Rotator
Rotate API keys from environment variables with configurable cooldown intervals.
Installation
pip install envrotate
Usage
import os
from envrotate import EnvRotate
# Set environment variables (in real use, set these externally)
os.environ["API_X_1"] = "key123"
os.environ["API_X_2"] = "key234"
os.environ["API_X_3"] = "key345"
# Initialize rotator
api_manager = EnvRotate(prefix="API_X_", min_interval=30)
# Get keys
print(api_manager.get(random=False, wait=True)) # "key123"
print(api_manager.get(random=True, wait=True)) # e.g., "key234"
print(api_manager.get(random=True, wait=True)) # e.g., "key345"
# After 30s cooldown, keys become available again
print(api_manager.get(random=True, wait=False)) # None (if <30s passed)
print(api_manager.get(random=True, wait=True)) # Waits then returns available key
API Reference
EnvRotate(prefix: str, min_interval: int)
Initializes the environment variable rotator.
prefix(str): Environment variable prefix to match (e.g., "API_X_"). Only environment variables starting with this prefix will be included in the rotation.min_interval(int): Minimum seconds before a key can be reused. This sets the cooldown period between key usage.
Raises:
ValueError: If no environment variables are found with the specified prefix.
get(random: bool = False, wait: bool = True) -> Optional[str]
Retrieves an available key from the pool.
random(bool, optional): IfTrue, selects randomly from currently available keys. IfFalse(default), uses round-robin selection starting with the key that became available earliest.wait(bool, optional): IfTrue(default), blocks and waits until a key is available. IfFalse, returnsNoneimmediately if no keys are available.
Returns:
strorNone: Returns an API key string when available. ReturnsNoneifwait=Falseand no keys are currently available.
Features
- Thread-safe: Uses locks for concurrent access
- Flexible selection: Round-robin or random key selection
- Automatic waiting: Sleeps until next key is available when wait=True
- Environment-driven: Reads keys directly from os.environ
Practical Example
from envrotate import EnvRotate
import os
# In a real application, set these as actual environment variables
os.environ["OPENAI_API_KEY_1"] = "sk-123..."
os.environ["OPENAI_API_KEY_2"] = "sk-456..."
os.environ["OPENAI_API_KEY_3"] = "sk-789..."
# Initialize with 60-second cooldown between key reuse
rotator = EnvRotate(prefix="OPENAI_API_KEY_", min_interval=60)
# Use round-robin selection (first available key)
api_key = rotator.get(random=False, wait=True)
# Use random selection from available keys
api_key = rotator.get(random=True, wait=True)
# Don't wait if no keys available (returns None immediately)
api_key = rotator.get(random=False, wait=False)
if api_key:
# Use the key
pass
else:
# Handle no available keys
print("No keys currently available")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
envrotate-0.1.1.tar.gz
(3.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file envrotate-0.1.1.tar.gz.
File metadata
- Download URL: envrotate-0.1.1.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67f760bef631fc1acf7dc7f00adf39f03528f2ff6fb143aa35d3e99f562bb669
|
|
| MD5 |
6818406d944f0610d4b57bd426e1f186
|
|
| BLAKE2b-256 |
c892d52fe449a5671592b7d15be1a834e407a50d953b13cf36325ea85968eddf
|
File details
Details for the file envrotate-0.1.1-py3-none-any.whl.
File metadata
- Download URL: envrotate-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd5d5fef32e37008bf1297366d76047d2c30a5098a8272790a1a6a0a86c2c508
|
|
| MD5 |
8abf50f104764a0d8276da33efd11a37
|
|
| BLAKE2b-256 |
d090ec1018ba18e04ae3136507d08f9aa905ba20d0fbc2db70e17e436fabc5b5
|