diff options
| author | Scott <[email protected]> | 2022-01-20 14:45:34 -0800 |
|---|---|---|
| committer | Scott <[email protected]> | 2022-01-20 14:45:34 -0800 |
| commit | 370fb0c3be5501b9e27c4cb7c96821acf094f932 (patch) | |
| tree | ec887c80037dab17785feeeea678bf2c68f59716 | |
| parent | f9a44edcbc221df7965d23313c0b3daca20775ad (diff) | |
Make the new cmd_showing_output select and display data from stderr in
addition to stdout.
| -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: |
