asyncio-throttle
Simple, easy-to-use throttler for asyncio.
Example
import time
import random
import asyncio
from asyncio_throttle import Throttler
async def worker(no, throttler, n):
for _ in range(n):
await asyncio.sleep(random.random() * 2)
async with throttler:
print(time.time(), Worker #%d: Bang! % no)
async def main():
throttler = Throttler(rate_limit=5)
tasks = [
loop.create_task(worker(no, throttler, 10))
for no in range(5)
]
await asyncio.wait(tasks)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Here I limited work rate to 5/sec while there are 5 workers.
And the result:
1508273760.3462772 Worker #2: Bang!
1508273760.590009 Worker #3: Bang!
1508273760.856431 Worker #0: Bang!
1508273761.0110679 Worker #2: Bang!
1508273761.086856 Worker #4: Bang!
1508273761.350699 Worker #3: Bang!
1508273761.5906 Worker #1: Bang!
1508273761.8655958 Worker #4: Bang!
1508273762.224158 Work
|