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?
I have answered a rather similar question.
ensure_future
schedules coro execution, but does not wait for its completion andrun_until_complete
does not wait for the completion of all futures.This should fix it:
Using
asyncio
you can write a simple decorator as@background
. Now you can write whatever insidefoo()
and the control-flow will not wait for its completion.Produces