1
0
forked from clan/clan-core

Re-added rlist.remove to counter busy loop

This commit is contained in:
Luis Hebendanz 2024-01-12 00:08:15 +01:00
parent 0941367bb3
commit 1d489438bd

View File

@ -18,17 +18,14 @@ class Log(Enum):
STDERR = 1
STDOUT = 2
BOTH = 3
NONE = 4
def handle_output(process: subprocess.Popen, log: Log) -> tuple[str, str]:
rlist = [process.stdout, process.stderr]
stdout_buf = b""
stderr_buf = b""
# Note: We need to read till the process is done, otherwise we might block
# forever because the process might be waiting for us to read from the pipe
# before it can continue and will block in the write call.
while True:
while process.poll() is None:
r, _, _ = select.select(rlist, [], [], 0)
def handle_fd(fd: IO[Any] | None) -> bytes:
@ -36,6 +33,7 @@ def handle_output(process: subprocess.Popen, log: Log) -> tuple[str, str]:
read = os.read(fd.fileno(), 4096)
if len(read) != 0:
return read
rlist.remove(fd)
return b""
ret = handle_fd(process.stdout)
@ -50,11 +48,6 @@ def handle_output(process: subprocess.Popen, log: Log) -> tuple[str, str]:
sys.stderr.buffer.write(ret)
sys.stderr.flush()
stderr_buf += ret
# Check if the process is still running
if process.poll() is not None:
break
return stdout_buf.decode("utf-8"), stderr_buf.decode("utf-8")
@ -79,7 +72,7 @@ def run(
)
stdout_buf, stderr_buf = handle_output(process, log)
# stdout_buf, stderr_buf = process.communicate()
# Wait for the subprocess to finish
rc = process.wait()
cmd_out = CmdOut(