Created
March 7, 2014 21:35
-
-
Save asvetlov/9420657 to your computer and use it in GitHub Desktop.
Running subprocess from nonmain thread
This file contains 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
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