0

I have to make a post request to an api, that api does some processing and takes around 5-10 mins to return. But I don't have to wait for that api to return. I just want to fire that api from my AWS Batch job and leave it.

def post_id(id):
   # this function does a post using requests library

async def submit_post_requests(id_list):
    loop = asyncio.get_event_loop()
    excutor =  concurrent.futures.ThreadPoolExecutor(THREAD_COUNT)
    for id in set(id_list):
        loop.run_in_executor(excutor, post_id, id)

if __name__ == '__main__':
    id_list = [1,2,3]
    loop = asyncio.get_event_loop()
    loop.run_until_complete(submit_post_requests(id_list))
    print('reached end of program')

My code looks something like the above. When I run this it's reaching till the end of the program and printing reached end of program but the program is not exiting. Any suggestions on how we can exit the program without waiting for the post request to finish.

I know it's not a good practise to not wait for the post return but I don't have to wait for it and trying to exit the batch as quickly as can.

I am using this stack overflow questions as reference: How could I use requests in asyncio?

The only issue is the program not exiting till the coroutine finishes.

  • It's better to use asyncio-based lib. [Example](https://stackoverflow.com/questions/63872924/how-can-i-send-an-http-request-from-my-fastapi-app-to-another-site-api/63881674#63881674) – alex_noname Jan 12 '21 at 11:22
  • @alex_noname That's good general advice, but I'm not sure it solves the OP's issue, which is to send a request without waiting for a response. (I'm not sure it can be reliably done at all.) – user4815162342 Jan 12 '21 at 13:05
  • @alex_noname even using the aiohttp, I still have to wait for it to exit. It doesn't work in my particular case. – Butcher Jan 12 '21 at 13:13
  • You're right. With both, you can use a short timeout, but this does not guarantee that the request will reach the server. But you can choose a period that will (almost) always work in the given conditions. – alex_noname Jan 12 '21 at 14:08

1 Answers1

0

Program not exited because of threadsstill executing request.

Aiohttp or another will not start in this manner.

I suggest create detached and forked processes for this operations.

eri
  • 3,133
  • 1
  • 23
  • 35