Reputation: 1
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.
Upvotes: 0
Views: 400
Reputation: 3504
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.
Upvotes: 0