From be5492b3b7dfeb0993bed61aadaa1414dc93b5e7 Mon Sep 17 00:00:00 2001 From: Jorrit Schaap <schaap@astron.nl> Date: Tue, 4 Oct 2016 10:00:52 +0000 Subject: [PATCH] Task #9931: added non-blocking readlines method --- LCS/PyCommon/subprocess.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/LCS/PyCommon/subprocess.py b/LCS/PyCommon/subprocess.py index e923c81cfa4..dfdb4aadf90 100644 --- a/LCS/PyCommon/subprocess.py +++ b/LCS/PyCommon/subprocess.py @@ -1,3 +1,4 @@ +import logging from datetime import datetime, timedelta from threading import Thread try: @@ -5,6 +6,8 @@ try: except ImportError: from queue import Queue, Empty # python 3.x +logger = logging.getLogger() + class PipeReader: ''' helper class to do non-blocking readline calls to a subprocess stdput or stderr pipe. @@ -24,7 +27,7 @@ class PipeReader: except Exception as e: logger.error(e) - def readline(self, timeout=None): + def __fill_line_buffer(self, timeout=None): start = datetime.now() while timeout==None or datetime.now() - start <= timedelta(seconds=timeout): try: @@ -34,6 +37,9 @@ class PipeReader: except Empty: pass + def readline(self, timeout=None): + self.__fill_line_buffer(timeout) + endline_idx = self.__line_buffer.find('\n') if endline_idx > -1: @@ -42,3 +48,12 @@ class PipeReader: return line return '' + def readlines(self, timeout=None): + self.__fill_line_buffer(timeout) + + lines = self.__line_buffer.split('\n') + if lines and not lines[-1].endswith('\n'): + self.__line_buffer = lines[-1] + lines = lines[:-2] + + return lines -- GitLab