Skip to content

Commit

Permalink
♻️ Simplify some code
Browse files Browse the repository at this point in the history
Should *not* lead to a busy loop thanks to the blocking I/O.

See #4993
  • Loading branch information
foosel committed Apr 25, 2024
1 parent 8018abd commit 8f9bd68
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/octoprint/util/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def call(
delimiter: bytes = b"\n",
buffer_size: int = -1,
logged: bool = True,
output_timeout: float = 0.5,
**kwargs,
) -> Tuple[Optional[int], List[str], List[str]]:
"""
Expand Down Expand Up @@ -203,10 +204,11 @@ def process_stderr(lines):
return process_lines(lines, self._log_stderr)

try:
while p.returncode is None:
all_stderr += process_stderr(p.stderr.readlines(timeout=0.5))
all_stdout += process_stdout(p.stdout.readlines(timeout=0.5))
p.commands[0].poll()
# read lines from stdout and stderr until the process is finished
while p.commands[0].poll() is None:
# this won't be a busy loop, the readline calls will block up to the timeout
all_stderr += process_stderr(p.stderr.readlines(timeout=output_timeout))
all_stdout += process_stdout(p.stdout.readlines(timeout=output_timeout))

finally:
p.close()
Expand Down

0 comments on commit 8f9bd68

Please sign in to comment.