Python Forum

Full Version: Fire and forget
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
Is there a way to run a function in a fire and forget manner in python using asyncio from a synchronous function

The code below doesn't work for me. the loop thsat I get from asyncio.get_event_loop() is not running.

Thanks for any pointer.
Terry


def fire_and_forget(task, *args, **kwargs):
    loop = asyncio.get_event_loop()
    if callable(task):
        return loop.run_in_executor(None, task, *args, **kwargs)
    else:    
        raise TypeError('Task must be a callable')

def foo():
    #asynchronous stuff here


fire_and_forget(foo)