Registry / http-networking / asyncio-throttle

asyncio-throttle

JSON →
library1.0.2pypypi✓ verified 23d ago

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-throttle
INSTALL
IMPORT
SIG · ASYNCIO-THROTTLE
A
asyncio-throttle
http-networkingpythonv1.0.2
Install
1.5s avg
Import
197ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.2 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.210s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.184s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

Throttler
from asyncio_throttle import Throttler

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.

import asyncio import time import random from asyncio_throttle import Throttler async def worker(no, throttler, n): for _ in range(n): # Simulate some async work before acquiring the throttle await asyncio.sleep(random.random() * 0.5) async with throttler: # This block will be rate-limited print(f'{time.time():.4f} Worker #{no}: Bang!') async def main(): # Limit to 5 'Bang!' messages per second across all workers throttler = Throttler(rate_limit=5) # Create 5 workers, each trying to 'Bang!' 3 times tasks = [ asyncio.create_task(worker(no, throttler, 3)) for no in range(5) ] await asyncio.gather(*tasks) if __name__ == '__main__': # Ensure to run in an event loop asyncio.run(main())
Debug
Known issues
gotchaForgetting to `await` a coroutine function will create a coroutine object but not schedule or run it, leading to silent failures or unexpected behavior. Always use `await` or `asyncio.create_task()` for coroutines.
fix
Ensure all coroutine calls are `await`ed or scheduled via `asyncio.create_task()`.
affects: All versions
gotchaPerforming blocking I/O operations (e.g., `requests.get()`, `time.sleep()`) directly within an `asyncio` event loop will block the entire loop, negating the benefits of asynchronous programming. Use async-native libraries (e.g., `aiohttp`, `httpx`, `asyncpg`) or offload blocking operations to a thread pool via `loop.run_in_executor()`.
fix
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.
affects: All versions
gotchaCPU-bound tasks executed directly in the event loop will block all other concurrent tasks. For computationally intensive work, use `loop.run_in_executor()` or `asyncio.to_thread()` (Python 3.9+) to run them in a separate thread or process pool, preventing the event loop from stalling.
fix
Offload CPU-bound work to `asyncio.to_thread()` or `loop.run_in_executor(None, blocking_function, *args)`.
affects: All versions
breakingChanges in `asyncio` implementation in Python 3.14+ may affect environments that relied on `nest_asyncio` for automatic async-to-sync conversion (e.g., Jupyter notebooks). Direct `await` calls for async methods are now often required, and implicit conversions may fail with `RuntimeError`.
fix
Use explicit `await` for all async function calls in affected environments. Avoid `asyncio.run()` in already running event loops.
affects: Python 3.14+
Errors
Common errors & fixes
ImportError: cannot import name 'Throttle' from 'asyncio_throttle'
The class is named 'Throttler', not 'Throttle'.
fix
from asyncio_throttle import Throttler
AttributeError: 'Throttler' object has no attribute 'acquire'
The 'Throttler' class does not have an 'acquire' method; it should be used as an async context manager.
fix
async with throttler:
    # your code here
TypeError: 'Throttler' object is not callable
Attempting to call a 'Throttler' instance directly, which is not supported.
fix
async with throttler:
    # your code here
ModuleNotFoundError: No module named 'asyncio_throttle'
The `asyncio-throttle` package is not installed in the current Python environment, or the environment where it's installed is not activated.
fix
Ensure the library is installed using pip: `pip install asyncio-throttle`. If using a virtual environment, activate it first.
AttributeError: module 'asyncio_throttle' has no attribute 'Throttler'
The `Throttler` class is imported incorrectly. It needs to be imported directly from the package, not accessed as an attribute of the top-level module when a direct import is intended.
fix
Change the import statement from `import asyncio_throttle` to `from asyncio_throttle import Throttler`.
Upgrade
Version history
1.0.2latest on PyPI · released Apr 7, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
24 hits · last 30 days
node
20
OpenAI (training)
1
Resources