To add a function to an already running event loop you can use:
asyncio.ensure_future(my_coro())
In my case I was using multithreading (threading
) alongside asyncio
and wanted to add a task to the event loop that was already running. For anyone else in the same situation, be sure to explicitly state the event loop (as one doesn’t exist inside a Thread
). i.e:
In global scope:
event_loop = asyncio.get_event_loop()
Then later, inside your Thread
:
asyncio.ensure_future(my_coro(), loop=event_loop)