diff --git a/LCS/PyCommon/subprocess.py b/LCS/PyCommon/subprocess.py index e923c81cfa4fb4778e9d99eb774489844b615e3a..dfdb4aadf90e57a43c54a5e22e5f836b841cf2d4 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