asyncio-throttle is a simple, easy-to-use library providing a throttler for asynchronous operations in Python. It's designed to limit the rate at which code blocks can be executed, primarily through an `async with` statement. The current stable version is 1.0.2, released in April 2021. It requires Python 3.6 or later.
pip install asyncio-throttleVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `Throttler` as an asynchronous context manager to limit the rate of operations across multiple concurrent tasks. It creates five workers that each attempt to print a message, but the `Throttler` ensures a maximum of 5 messages are printed per second in total.
Ensure all coroutine calls are `await`ed or scheduled via `asyncio.create_task()`.
Replace blocking I/O with async equivalents or use `asyncio.to_thread()` (Python 3.9+) / `loop.run_in_executor()` for CPU-bound or blocking tasks.
Offload CPU-bound work to `asyncio.to_thread()` or `loop.run_in_executor(None, blocking_function, *args)`.
Use explicit `await` for all async function calls in affected environments. Avoid `asyncio.run()` in already running event loops.
from asyncio_throttle import Throttler
async with throttler:
# your code hereasync with throttler:
# your code hereEnsure the library is installed using pip: `pip install asyncio-throttle`. If using a virtual environment, activate it first.
Change the import statement from `import asyncio_throttle` to `from asyncio_throttle import Throttler`.
No dependency data recorded yet.