Created
May 7, 2018 12:52
-
-
Save mivade/27df9a5f600bd2f8b607c03d241968cf to your computer and use it in GitHub Desktop.
Running coroutines without explicitly awaiting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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