Issue
Not sure if that's achievable. I want to fire an HTTP POST request from a script, but not wait for a response. Instead I want to return immediately.
I tries something along these lines:
#!/usr/bin/env python3
import asyncio
import aiohttp
async def fire():
await client.post('http://httpresponder.com/xyz')
async def main():
asyncio.ensure_future(fire())
if __name__ == '__main__':
loop = asyncio.get_event_loop()
client = aiohttp.ClientSession(loop=loop)
loop.run_until_complete(main())
The script returns immediately without errors, but the HTTP request never arrives at the destination. Can I fire the POST request, but not wait for response from the server, just terminate the moment the request is sent?
Solution
I have answered a rather similar question.
async def main():
asyncio.ensure_future(fire())
ensure_future
schedules coro execution, but does not wait for its completion and run_until_complete
does not wait for the completion of all futures.
This should fix it:
async def main():
await fire()
Answered By - Artemiy Rodionov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.