Skip to content

Instantly share code, notes, and snippets.

@oconnor663
Last active August 26, 2015 03:31
Show Gist options
  • Select an option

  • Save oconnor663/f0ddad2c0bd1f7cf14c2 to your computer and use it in GitHub Desktop.

Select an option

Save oconnor663/f0ddad2c0bd1f7cf14c2 to your computer and use it in GitHub Desktop.
run asyncio without disrupting other code?
import asyncio
import contextlib
@asyncio.coroutine
def my_coroutine(loop, text):
p = yield from asyncio.create_subprocess_shell("echo " + text, loop=loop)
yield from p.communicate()
def my_function():
loop = asyncio.new_event_loop()
watcher = asyncio.SafeChildWatcher()
asyncio.set_child_watcher(watcher)
watcher.attach_loop(loop)
with contextlib.closing(watcher):
loop.run_until_complete(my_coroutine(loop, "in func"))
asyncio.get_event_loop().run_until_complete(my_coroutine(None, "toplevel"))
my_function()
# BUG! This hangs.
asyncio.get_event_loop().run_until_complete(my_coroutine(None, "toplevel"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment