Home > OS >  Asyncio - how to limit requests per 1 second?
Asyncio - how to limit requests per 1 second?

Time:09-28

I'm new to asyncio, trying to make async calls to API, but when I'm sending more than 1 request/second the API responding with 429 status code - too many requests... According to API documentation, I should be doing only 1 req/sec.

Can not figure out how to do only 1 request per second for this code:

#Call to API
async def ps_request_marshal(converted_urls, device_input):
    global device_settings, data, tasks
    data = []
    device_settings = device_input
    tasks = []
    url = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={}' '&key= key &strategy='  device_input

    async with aiohttp.ClientSession() as session:
        for page in converted_urls:
            tasks.append(asyncio.create_task(session.get(url.format(page), ssl=False)))
        responses = await asyncio.gather(*tasks)
        for response in responses:
            data.append(await response.json())
        print(responses)

Will much appreciate your help!

CodePudding user response:

Solved, just added 'await asyncio.sleep(delay_per request)' to loop of task creation.

async def ps_request_marshal(converted_urls, device_input):
    global device_settings, data, tasks
    data = []
    device_settings = device_input
    tasks = []
    url = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={}' '&key=key='  device_input
    delay_per_request = 1
    async with aiohttp.ClientSession() as session:
        for page in converted_urls:
            tasks.append(asyncio.create_task(session.get(url.format(page), ssl=False)))
            await asyncio.sleep(delay_per_request)
        responses = await asyncio.gather(*tasks)
        print(responses)
        for response in responses:
            data.append(await response.json())
        print(responses)
    
  • Related