This gist consists of 4 files:
throttler.py - Throttler class implementation
asyncio_throttling_example.py - Throttling general coroutines
aiohttp_throttling_example_client.py - Throttling aiohttp requests
aiohttp_throttling_example_server.py - Web server for testing aiohttp_throttling_example_client.py
The main Throttler
class's implementation is easy to understand, so take a look at it. It (may)supports Python >=3.5.
You can create Throttler
instance with proper rate limit:
from throttler import Throttler
throttler = Throttler(50) # Limit rate to 50 tasks/sec
You can use multiple context expressions in with
statement(Of course, we'll use async with
in this case):
async with throttler, session.get('http://httpbin.org') as resp:
print(await resp.text())