Skip to content

Instantly share code, notes, and snippets.

@mivade
Created May 7, 2018 12:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mivade/27df9a5f600bd2f8b607c03d241968cf to your computer and use it in GitHub Desktop.
Save mivade/27df9a5f600bd2f8b607c03d241968cf to your computer and use it in GitHub Desktop.
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