Throttler for asyncio Python
Project description
aiothrottler
Throttler for asyncio Python
Installation
pip install aiothrottler
Example: multiple tasks throttled
import asyncio
import time
from aiothrottler import Throttler
async def main():
throttler = Throttler(0.5)
await asyncio.gather(*[
worker(throttler) for _ in range(10)
])
async def worker(throttler):
await throttler()
# Interval of at least 0.5 seconds between prints
# even though all workers started together
print(time.time())
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Example: single task throttled/smoothed
import asyncio
import random
import time
from aiothrottler import Throttler
async def main():
throttler = Throttler(0.5)
for _ in range(10):
await throttler()
# Interval of at least 0.5 seconds between prints
# even though each sleep is random
print(time.time())
await asyncio.sleep(random.random())
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Differences to alternatives
-
The API features a function to call to
await
its result [some use a context manager] -
The API is imperative [some use a functional approach/higher-order function]
-
No polling is used [some use polling internally]
-
A minimum interval between resolutions is used to throttle [rather that a max resolutions per time interval, which can cause an irregular pattern of resolutions]
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
aiothrottler-0.0.10.tar.gz
(2.2 kB
view hashes)
Built Distribution
Close
Hashes for aiothrottler-0.0.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 785ef4bc6c50e01ffb7dbc7aceeea9876b89d373689cddc877cd59a2ea3bfd00 |
|
MD5 | ad68f5dc733dd72c08c027ea8827427a |
|
BLAKE2b-256 | 1acb3f3199f9d509d3b1921de709a78c5c739049ebba41e506081a22a42c025d |