forked from clan/clan-core
Re-added rlist.remove to counter busy loop
This commit is contained in:
parent
0941367bb3
commit
1d489438bd
@ -18,17 +18,14 @@ class Log(Enum):
|
|||||||
STDERR = 1
|
STDERR = 1
|
||||||
STDOUT = 2
|
STDOUT = 2
|
||||||
BOTH = 3
|
BOTH = 3
|
||||||
|
NONE = 4
|
||||||
|
|
||||||
def handle_output(process: subprocess.Popen, log: Log) -> tuple[str, str]:
|
def handle_output(process: subprocess.Popen, log: Log) -> tuple[str, str]:
|
||||||
rlist = [process.stdout, process.stderr]
|
rlist = [process.stdout, process.stderr]
|
||||||
stdout_buf = b""
|
stdout_buf = b""
|
||||||
stderr_buf = b""
|
stderr_buf = b""
|
||||||
|
|
||||||
# Note: We need to read till the process is done, otherwise we might block
|
while process.poll() is None:
|
||||||
# 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:
|
|
||||||
r, _, _ = select.select(rlist, [], [], 0)
|
r, _, _ = select.select(rlist, [], [], 0)
|
||||||
|
|
||||||
def handle_fd(fd: IO[Any] | None) -> bytes:
|
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)
|
read = os.read(fd.fileno(), 4096)
|
||||||
if len(read) != 0:
|
if len(read) != 0:
|
||||||
return read
|
return read
|
||||||
|
rlist.remove(fd)
|
||||||
return b""
|
return b""
|
||||||
|
|
||||||
ret = handle_fd(process.stdout)
|
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.buffer.write(ret)
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
stderr_buf += ret
|
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")
|
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 = handle_output(process, log)
|
||||||
# stdout_buf, stderr_buf = process.communicate()
|
|
||||||
# Wait for the subprocess to finish
|
# Wait for the subprocess to finish
|
||||||
rc = process.wait()
|
rc = process.wait()
|
||||||
cmd_out = CmdOut(
|
cmd_out = CmdOut(
|
||||||
|
Loading…
Reference in New Issue
Block a user