Skip to content

Instantly share code, notes, and snippets.

@Sraw
Created December 25, 2017 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sraw/803c0a7ffec327c765610c72dca732c2 to your computer and use it in GitHub Desktop.
Save Sraw/803c0a7ffec327c765610c72dca732c2 to your computer and use it in GitHub Desktop.
Run asyncio event loop in sub-thread

asyncio won't automatically create event loop for sub-thread, so to run async tasks in sub-thread, we need to manually create an event loop first.

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
asyncio.ensure_future(task())
loop.run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment