Skip to content

Instantly share code, notes, and snippets.

@asvetlov
Created March 7, 2014 21:35
Show Gist options
  • Save asvetlov/9420657 to your computer and use it in GitHub Desktop.
Save asvetlov/9420657 to your computer and use it in GitHub Desktop.
Running subprocess from nonmain thread
from asyncio import *
@coroutine
def coro():
proc = yield from create_subprocess_exec('true')
yield from proc.wait()
print('subprocess returncode', proc.returncode)
def thr():
subloop = new_event_loop()
set_event_loop(subloop)
try:
subloop.run_until_complete(coro())
finally:
subloop.close()
@coroutine
def main():
return main_loop.run_in_executor(None, thr)
main_loop = get_event_loop()
get_child_watcher() # !!! Required for attaching child watcher to main loop
try:
main_loop.run_until_complete(main())
finally:
main_loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment