From 844c6f764579bb406155db015428dca45798f84b 2020-02-26 18:35:24 From: Itamar Turner-Trauring Date: 2020-02-26 18:35:24 Subject: [PATCH] Try to avoid blocking in read(). --- diff --git a/IPython/utils/_process_win32.py b/IPython/utils/_process_win32.py index 13a1aed..6d05bda 100644 --- a/IPython/utils/_process_win32.py +++ b/IPython/utils/_process_win32.py @@ -23,6 +23,7 @@ import time from ctypes import c_int, POINTER from ctypes.wintypes import LPCWSTR, HLOCAL from subprocess import STDOUT, TimeoutExpired +from threading import Thread # our own imports from ._process_common import read_no_interrupt, process_handler, arg_split as py_arg_split @@ -94,20 +95,25 @@ def _find_cmd(cmd): def _system_body(p): """Callback for _system.""" enc = DEFAULT_ENCODING - print("READING...") - for line in read_no_interrupt(p.stdout).splitlines(): - line = line.decode(enc, 'replace') - print(line, file=sys.stdout) - for line in read_no_interrupt(p.stderr).splitlines(): - line = line.decode(enc, 'replace') - print(line, file=sys.stderr) + + def stdout_read(): + for line in read_no_interrupt(p.stdout).splitlines(): + line = line.decode(enc, 'replace') + print(line, file=sys.stdout) + + def stderr_read(): + for line in read_no_interrupt(p.stderr).splitlines(): + line = line.decode(enc, 'replace') + print(line, file=sys.stderr) + + Thread(target=stdout_read).start() + Thread(target=stderr_read).start() # Wait to finish for returncode. Unfortunately, Python has a bug where # wait() isn't interruptible (https://bugs.python.org/issue28168) so poll in # a loop instead of just doing `return p.wait()`. while True: result = p.poll() - print("POLLED") if result is None: time.sleep(0.01) else: