-
The API I am trying against has a limit of 250 requests per minute. Whatever I try I get 429 responses, it doesnt seep to "sleep" or "slow down" after 250 requests async def main():
limiter = AsyncLimiter(250, 60)
async with client as cl: # httpx async client
# for i in range(400):
# async with limiter:
async with limiter:
for i in range(400):
await cl.get(url)
asyncio.run(main()) Furthermore, the code I am trying to make use of your library has this form: async def get_name():
# api request
...
async def get_last_name():
# another api request
...
async def get_user_data():
name = await get_name()
last_name = await get_last_name()
return name, last_name
async def main():
coros = [get_user_data() for user in many_users()]
for result in asyncio.gather(*coros):
# handle result Is it possible somehow to apply the limiter in such structure? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Your error lies here:
The whole block under for i in range(400):
async with limiter:
await cl.get(url) Now execution passes from
Create a limiter in async def get_user_data(limiter):
with limiter:
name = await get_name()
last_name = await get_last_name()
return name, last_name
async def main():
limiter = AsyncLimiter(250, 60)
coros = [get_user_data(limiter) for user in many_users()]
for result in asyncio.gather(*coros):
# handle result |
Beta Was this translation helpful? Give feedback.
-
Thanks for your response @mjpieters. I tried what you suggest as well, if you see in my post I have your suggestion as comment. I am going to give it another try again. |
Beta Was this translation helpful? Give feedback.
-
The limiter will allow the first 250 requests through at once, see the Bursting section of the documentation; requests are not automatically spread out over the time period. If you are still not seeing a limit on the number of requests in the time period you set, you are not yet correctly using the limiter. Try the example in the 'bursting' section I linked to to see the limiter in operation. |
Beta Was this translation helpful? Give feedback.
Your error lies here:
The whole block under
async with limiter:
is limited to 256 executions per 60 seconds, but you loop inside that block. Put the loop outside of the limiter:Now execution passes from
for
to theasync with limiter:
block, which can then control how fastawait cl.get(url)
is called.Create a limiter in
main()
and pass it intoget_user_data()
so it is shared between coroutines: