Registry / http-networking / pyrate-limiter

pyrate-limiter

JSON →
library4.4.0pypypi✓ verified 26d ago

PyrateLimiter is a Python library implementing the Leaky-Bucket Algorithm for rate limiting. It supports both synchronous and asynchronous workflows and offers various backends like in-memory, SQLite, Redis, and PostgreSQL for persistent limit tracking. Currently at version 4.1.0, it maintains an active development and release cadence, with several minor and patch releases in the last year.

pip install pyrate-limiter
INSTALL
IMPORT
SIG · PYRATE-LIMITER
P
pyrate-limiter
http-networkingpythonv4.4.0
Install
1.6s avg
Import
435ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.477s · 18.1MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.393s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Limiter
from pyrate_limiter import Limiter
from pyrate_limiter.limiter import Limiter
While functional, importing directly from the top-level package is the recommended and cleaner approach.
Rate
from pyrate_limiter import Duration, Rate
from pyrate_limiter import RequestRate
`RequestRate` was renamed to `Rate` in v4.0.0.

This quickstart demonstrates how to set up a basic in-memory rate limiter with a single rate. It shows both blocking and non-blocking acquisition attempts.

from pyrate_limiter import Duration, Rate, Limiter # Limit 5 requests within 2 seconds limiter = Limiter(Rate(5, Duration.SECOND * 2)) # Blocking mode (default) - waits until permit available for i in range(6): limiter.try_acquire(str(i)) print(f"Acquired permit {i}") # Non-blocking mode - returns False if bucket full for i in range(6): success = limiter.try_acquire(str(i), blocking=False) if not success: print(f"Rate limited at {i}")
Debug
Known issues
breakingThe `Limiter` constructor API changed significantly in v4.0.0. It now expects a single `Bucket` instance or a `BucketFactory` instead of a list of `Rate` objects directly. The `RequestRate` class was also renamed to `Rate`.
fix
Update `Limiter` initialization: `limiter = Limiter(Rate(X, Duration.Y))` for single rates or use `limiter = Limiter(BucketFactory(...))` for complex scenarios. Replace `RequestRate` with `Rate`.
affects: >=4.0.0
breakingThe `max_delay` argument for configuring rate limit delays in the `Limiter` constructor was removed in v4.0.0. Delay behavior is now integrated or configured differently at the bucket level.
fix
Review the documentation for the specific bucket type or `Limiter` methods to configure delay behavior in v4.x, as `max_delay` is no longer a direct constructor argument.
affects: >=4.0.0
gotchaFor Redis or PostgreSQL backends, you must install the optional dependencies (e.g., `pyrate-limiter[redis]` or `pyrate-limiter[postgres]`). Without them, these backends will not be available.
fix
Install with the appropriate extra: `pip install pyrate-limiter[redis]` for Redis, or `pip install pyrate-limiter[postgres]` for PostgreSQL.
affects: All
gotchaWhen defining multiple rates for a single limiter, they must be ordered correctly: by interval length (least to greatest) and by the ratio of limit/interval (greatest to least). Incorrect ordering can lead to unexpected rate limiting behavior.
fix
Ensure `Rate` objects passed to a bucket or factory are sorted according to the specified rules.
affects: All
gotchaThe default `InMemoryBucket` does not share state across multiple processes or distributed environments. For multiprocessing or distributed applications, use persistent backends like `SQLiteBucket` (with `use_file_lock=True`) or `RedisBucket`/`PostgresBucket`.
fix
Switch to a persistent backend (`SQLiteBucket`, `RedisBucket`, `PostgresBucket`) and configure it appropriately for your distributed setup.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pyrate_limiter'
The 'pyrate-limiter' library is not installed in the current Python environment.
fix
pip install pyrate-limiter
SyntaxError: invalid token
Python module names cannot contain hyphens, so the 'pyrate-limiter' package must be imported using its underscore-separated module name 'pyrate_limiter'.
fix
from pyrate_limiter import RateLimiter, Rate, Limiter, MemoryBackend
AttributeError: 'str' object has no attribute 'set'
The `RedisBackend` expects an initialized `redis.Redis` client object, but received a string (e.g., connection details) instead.
fix
import redis
from pyrate_limiter import RedisBackend

redis_client = redis.Redis(host='localhost', port=6379)
backend = RedisBackend(redis_client)
RuntimeWarning: coroutine 'RateLimiter.try_acquire' was never awaited
An asynchronous method `try_acquire` was called without the `await` keyword in an `async` function, preventing its execution and proper handling.
fix
async def my_async_function():
    # ... (setup limiter)
    if await limiter.try_acquire("user1", Rate(10, 60)):
        print("Request allowed")
    else:
        print("Too many requests")
Upgrade
Version history
4.4.0latest on PyPI · released Jun 14, 2026
Audit
Dependencies
filelockoptionalRequired for MultiProcessBucket with SQLite file locking.
redisoptionalRequired for the Redis backend.
psycopgoptionalRequired for the PostgreSQL backend.
Agent activity
5 hits · last 30 days
node
4
Resources
pyrate-limiter — pip install pyrate-limiter · libregistry