Sometimes there is some non-critical asynchronous operation that needs to happen but I don\'t want to wait for it to complete. In Tornado\'s coroutine implementation you ca
Thank you Sergey for the succint answer. Here is the decorated version of the same.
import asyncio
import time
def fire_and_forget(f):
def wrapped(*args, **kwargs):
return asyncio.get_event_loop().run_in_executor(None, f, *args, *kwargs)
return wrapped
@fire_and_forget
def foo():
time.sleep(1)
print("foo() completed")
print("Hello")
foo()
print("I didn't wait for foo()")
Produces
>>> Hello
>>> foo() started
>>> I didn't wait for foo()
>>> foo() completed
Note: Check my other answer which does the same using plain threads.