Python Redis Rate Limit
raw JSON → 0.0.10 verified Sat May 09 auth: no python maintenance
A Python rate limiter based on Redis using a sliding window algorithm. It provides a decorator and context manager to limit the rate of function calls or code blocks. Version 0.0.10 is the latest release, with no recent updates; the package appears to be in maintenance mode.
pip install python-redis-rate-limit Common errors
error ModuleNotFoundError: No module named 'rate_limit' ↓
cause Trying to import rate_limit incorrectly (e.g., 'from python_redis_rate_limit import rate_limit' or 'import rate_limit') without understanding the package structure.
fix
Install the package and import correctly:
from rate_limit import rate_limit. The module name is 'rate_limit' (not 'python_redis_rate_limit'). error redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection refused. ↓
cause Redis server is not running or not accessible at the specified host/port.
fix
Start the Redis server:
redis-server. Alternatively, use environment variables to configure the connection, e.g., redis.Redis.from_url(os.environ.get('REDIS_URL', 'redis://localhost:6379')). Warnings
gotcha The rate_limit decorator uses a sliding window, but requires a Redis instance. If Redis is down, the decorator will raise redis.exceptions.ConnectionError. The library does not handle connection failures gracefully. ↓
fix Wrap decorated calls in try-except or implement a fallback mechanism (e.g., check Redis availability before decorating).
deprecated In v0.0.9, deprecated distutils.version was removed. This may affect users relying on Python 2.7 support (which was also removed). The library now requires Python 3. ↓
fix Upgrade to Python 3 and use v0.0.9 or later.
gotcha The package uses a global redis client reference; if multiple decorators or context managers are used with different Redis instances, the wrong instance may be used due to how the library stores the connection internally. ↓
fix Instantiate a new rate limiter object locally or ensure only one Redis instance is used. Check the source code for details.
Imports
- rate_limit
from rate_limit import rate_limit
Quickstart
import redis
from rate_limit import rate_limit
r = redis.Redis(host='localhost', port=6379, db=0)
@rate_limit(r, max_calls=10, period=60)
def my_function():
print('Function called')
for _ in range(12):
my_function()