I have a function in Google Cloud that accepts a number of parameters. I generate ~2k asynchronous requests with different combinations of parameter values using aiohttp:
# url = 'https://...'
# headers = {'X-Header': 'value'}
timeout = aiohttp.ClientTimeout(total=72000000)
async def submit_bt(session, url, payload):
async with session.post(url, json=payload) as resp:
result = await resp.text()
async def main():
async with aiohttp.ClientSession(headers=headers, timeout=timeout) as session:
tasks = []
gen = payload_generator() # a class that generates dictionaries
for payload in gen.param_grid():
tasks.append(asyncio.ensure_future(submit_bt(session, url, payload)))
bt_results = await asyncio.gather(*tasks)
for result in bt_results:
pass
asyncio.run(main())
A function takes between 3 to 6 minutes to run, function timeout is set to 9 minutes and maximum number of instances to 3000, but I never see more than 150-200 instances being initiated even when the total number of submitted requests is between 1.5k and 2.5k. On some occasions all requests are processed in 20 to 30 minutes, but sometimes I get an error on the client side:
ClientOSError: [Errno 104] Connection reset by peer
that does not correspond to any errors on the server side. I think I should be able to catch it as an aiohttp.client_exceptions.ClientOSError exception, but I am not sure how to handle it in the asynchronous settings, so that the failed request is resubmitted and the premature termination is avoided. Any hints are greatly appreciated.