A lightweight, zero-dependency Python library to limit concurrent execution and queue size of asynchronous tasks — simulating a TCP-style backlog for async servers, job runners, or microservices to ensure overload protection.
pip install async-backlog-limiter
- If more than
capacityconcurrent requests are running, additional ones will wait. - If the
total number of active + waitingrequests exceedsqueue_limit, new ones are immediately rejected with aRateLimitExceededexception.
import asyncio
from async_backlog_limiter import AsyncBacklogLimiter, RateLimitExceeded
limiter = AsyncBacklogLimiter(capacity=5, queue_limit=10)
async def handle_request():
try:
async with limiter():
# Your async work here
await asyncio.sleep(1)
except RateLimitExceeded:
print("Request rejected due to overload.")
# Run many tasks
await asyncio.gather(*[handle_request() for _ in range(20)])
# Get current statistics about the limiter's state
limiter.stats()This project includes a comprehensive test suite using unittest.
To run tests:
python -m unittest discover -s tests