From 370fb0c3be5501b9e27c4cb7c96821acf094f932 Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 20 Jan 2022 14:45:34 -0800 Subject: [PATCH] Make the new cmd_showing_output select and display data from stderr in addition to stdout. --- exec_utils.py | 23 ++++++++++++++++++----- 1 file 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: -- 2.45.2