Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

python - fire and forget a post request using asyncio

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.

question from:https://stackoverflow.com/questions/65682143/fire-and-forget-a-post-request-using-asyncio

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...