diff options
Diffstat (limited to 'exec_utils.py')
| -rw-r--r-- | exec_utils.py | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/exec_utils.py b/exec_utils.py index 36d48fa..edbd21f 100644 --- a/exec_utils.py +++ b/exec_utils.py @@ -2,6 +2,7 @@ import atexit import logging +import selectors import shlex import subprocess import sys @@ -21,11 +22,23 @@ def cmd_showing_output(command: str) -> None: stderr=subprocess.PIPE, universal_newlines=False, ) - for char in iter(lambda: p.stdout.read(1), b''): - sys.stdout.buffer.write(char) - if char in line_enders: - sys.stdout.flush() - p.wait() + sel = selectors.DefaultSelector() + sel.register(p.stdout, selectors.EVENT_READ) + sel.register(p.stderr, selectors.EVENT_READ) + while True: + for key, _ in sel.select(): + char = key.fileobj.read(1) + if not char: + p.wait() + return + if key.fileobj is p.stdout: + sys.stdout.buffer.write(char) + if char in line_enders: + sys.stdout.flush() + else: + sys.stderr.buffer.write(char) + if char in line_enders: + sys.stderr.flush() def cmd_with_timeout(command: str, timeout_seconds: Optional[float]) -> int: |
