Rock solid async python generic distributed rate limiters (concurrency and time) backed by Redis.
Project description
async-redis-rate-limiters
Rock solid async python generic distributed rate limiters (concurrency and time) backed by Redis.
[!WARNING]
This is a very preliminary version of the library and only concurrency limiters are available for now.
Features
- ✅ Support very high concurrency (>100K), keep a reasonable number of connections to Redis (default: 300)
- ✅ Rock solid with Redis/Network failures (multiple attempts, exponential backoff, etc.)
- you can restart the Redis server during the execution without any exception or losing any semaphore! (of course, if persistence is setup in the redis instance)
- ✅ Very high performances with almost no polling at all
- ✅ Memory backend (for testing)
Non-features
- ❌ No time based rate limiters (yet)
- ❌ No blocking support, only async Python
Installation
pip install async-redis-rate-limiters
(or same with your favorite package manager)
Usage
import asyncio
from typing import AsyncContextManager
from async_redis_rate_limiters import DistributedSemaphoreManager
async def worker(semaphore: AsyncContextManager):
async with semaphore:
# concurrency limit enforced here
pass
async def main():
manager = DistributedSemaphoreManager(
redis_url="redis://localhost:6379",
redis_max_connections=100,
)
# Limit the concurrency to 10 concurrent tasks for the key "test"
semaphore = manager.get_semaphore("test", 10)
tasks = [asyncio.create_task(worker(semaphore)) for _ in range(1000)]
await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main())
What about if you want to use the memory backend?
WARNING: the memory backend is just a wrapper on a classic asyncio.Semaphore, it is not "distributed" at all!
manager = DistributedSemaphoreManager(
backend = "memory"
)
# and use it classically
Dev
- Lint the code:
make lint
- Run the tests:
make test
note: you need a redis instance listening to localhost:6379
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
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 async_redis_rate_limiters-0.0.8.tar.gz.
File metadata
- Download URL: async_redis_rate_limiters-0.0.8.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0067a13b0b9a11002d724c0b852cd2fd5641a5293b68b6d57eaa7d3d5e366df4
|
|
| MD5 |
aada71bfdcf8cfe8bdd3c1d342e8ddc1
|
|
| BLAKE2b-256 |
a57c2d98209afe5d02b03ba2d462fa4055f15594be8720769de9a8d27b04cce3
|
File details
Details for the file async_redis_rate_limiters-0.0.8-py3-none-any.whl.
File metadata
- Download URL: async_redis_rate_limiters-0.0.8-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f69a318cd15aa4973fca31018cdf9c73bf92ad6b9221d4f50ab2b6c75b010f6
|
|
| MD5 |
60c1ee8e5b1fee3eda9745e2a6e5afce
|
|
| BLAKE2b-256 |
ebca3a0abdc6f191bc1c6ff12369a87f9311fe6d8c42b15fb22ecc30ce958745
|