Simple, easy-to-use throttler for asyncio
Project description
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.
Installation
$ pip install asyncio-throttle
Usage
asyncio_throttle.Throttler introduces simple APIs: flush() and acquire(). But you will not be interested in those because you can just use it within with statement and it looks nicer.
First, create a throttler given desired rate limit. For example if you want to limit rate to 500/min, you can make it as:
from asyncio_throttle import Throttler
throttler = Throttler(rate_limit=500, period=60)
Then whenever you want to do some jobs which should have limited rate(e.g. sending request to server), Put it in async with statement:
async with throttler:
send_a_request()
It’s that easy. asyncio_throttler can be easily integrated with aiohttp too:
async def worker(throttler, session):
while True:
async with throttler:
async with session.get('http://example.com') as resp:
do_some_job_with(await resp.text())
await asyncio.sleep(0.05)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for asyncio_throttle-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e639d07b4f0431c3090ca7c582a6e12e695a00889eefee3206cd5b96f8910b88 |
|
MD5 | 57d03a89147ec088591a6ae9ab0d2250 |
|
BLAKE2b-256 | 3def88e1a2fd571a551d50ec20158e78e481ec050227e0ef9e23b1c8632e488e |