what about aiohttp.ClientSession? #71
-
Unless I am doing something wrong, it wont work with that.
Can you provide an example? Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
You are gating access to the Gather schedules each entry as a separate task, so they run in parallel independently. You are only rate limiting access to the You can share the limiter between tasks; you could pass it in as an argument: async def fetch(limiter, something):
async with limiter:
await get(something)
await asyncio.gather(*[fetch(limiter, t) for t in range(100)]) So when each task runs, each task has to first ask for the rate limiter if it can run the Note that the Bursting section in the documentation uses tasks too; the limiter is used inside the task coroutine. |
Beta Was this translation helpful? Give feedback.
-
Right, for now I am resorting to using my own limiter routine, but I still hope that this package gets fixed import asyncio
from datetime import datetime, timedelta
from aiohttp import ClientSession
async def limitCalls(tasks: list, max_rate=1, interval=0):
n = 0
results = []
while _tasks := tasks[n:n+max_rate]:
n += max_rate
t = datetime.now() + timedelta(seconds=interval)
results.extend(await asyncio.gather(*_tasks))
if n < len(tasks):
block = (t - datetime.now()).total_seconds()
block > 0 and await asyncio.sleep(block)
return results
async def fetch(n):
async with ClientSession() as session:
async with session.get(f"https://www.google.co.uk/SORRY_GOOGLE/{n}") as resp:
print(datetime.now().strftime("%H:%M:%S"), resp.status)
return await resp.text()
l = asyncio.get_event_loop()
l.run_until_complete(limitCalls([fetch(n) for n in range(10)], 3, 5))
### EXECUTION
time ./main.py
08:57:30 404
08:57:30 404
08:57:30 404
08:57:35 404
08:57:35 404
08:57:35 404
08:57:40 404
08:57:40 404
08:57:40 404
08:57:45 404
real 0m15.211s |
Beta Was this translation helpful? Give feedback.
You are gating access to the
asyncio.gather()
call. You haven't made it explicit, but I'm going to assume that that's not what you wanted, but that you instead wanted to limitget()
calls.Gather schedules each entry as a separate task, so they run in parallel independently. You are only rate limiting access to the
asyncio.gather()
call, and each time execution has reached that line, 100 tasks are created. Those tasks are themselves not rate limited. If you want to rate limit theget()
calls, put yourwith
block there, inside the task function.You can share the limiter between tasks; you could pass it in as an argument: