From b5f40cfd693a94816f50c90b18c25b54de819b7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 11 Feb 2016 10:27:07 +0100 Subject: [PATCH] Always read all available lines from stdout and stderr Should produce faster output. --- src/octoprint/util/pip.py | 44 +++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/octoprint/util/pip.py b/src/octoprint/util/pip.py index 217609b5..8c88f8eb 100644 --- a/src/octoprint/util/pip.py +++ b/src/octoprint/util/pip.py @@ -124,34 +124,34 @@ class PipCaller(object): all_stderr = [] try: while p.returncode is None: - line = p.stderr.readline(timeout=0.5) - if line: - line = self._convert_line(line) - self._log_stderr(line) - all_stderr.append(line) + lines = p.stderr.readlines(timeout=0.5) + if lines: + lines = self._convert_lines(lines) + self._log_stderr(*lines) + all_stderr += lines - line = p.stdout.readline(timeout=0.5) - if line: - line = self._convert_line(line) - self._log_stdout(line) - all_stdout.append(line) + lines = p.stdout.readlines(timeout=0.5) + if lines: + lines = self._convert_lines(lines) + self._log_stdout(*lines) + all_stdout += lines p.commands[0].poll() finally: p.close() - stderr = p.stderr.text - if stderr: - split_lines = map(self._convert_line, stderr.split("\n")) - self._log_stderr(*split_lines) - all_stderr += split_lines + lines = p.stderr.readlines() + if lines: + lines = map(self._convert_line, lines) + self._log_stderr(*lines) + all_stderr += lines - stdout = p.stdout.text - if stdout: - split_lines = map(self._convert_line, stdout.split("\n")) - self._log_stdout(*split_lines) - all_stdout += split_lines + lines = p.stdout.readlines() + if lines: + lines = map(self._convert_line, lines) + self._log_stdout(*lines) + all_stdout += lines return p.returncode, all_stdout, all_stderr @@ -240,6 +240,10 @@ class PipCaller(object): def _log_stderr(self, *lines): self.on_log_stderr(*lines) + @staticmethod + def _convert_lines(lines): + return map(PipCaller._convert_line, lines) + @staticmethod def _convert_line(line): return to_unicode(_clean_ansi(line), errors="replace")