Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Running coroutines without explicitly awaiting
import asyncio
from threading import Event, Thread
class EventLoopThread(Thread):
def __init__(self):
super().__init__()
self.loop = None
self.ready = Event()
def run(self):
self.loop = asyncio.new_event_loop()
self.ready.set()
self.loop.run_forever()
async def hello(name):
print("hello", name)
thread = EventLoopThread()
thread.start()
thread.ready.wait()
asyncio.run_coroutine_threadsafe(hello('world'), thread.loop)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment