Skip to content
Snippets Groups Projects
Commit c842ccb2 authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

Task #9931: added helper class to do non-blocking readline calls to a...

Task #9931: added helper class to do non-blocking readline calls to a subprocess stdput or stderr pipe
parent e62bcf9f
No related branches found
No related tags found
No related merge requests found
...@@ -2683,6 +2683,7 @@ LCS/PyCommon/__init__.py -text ...@@ -2683,6 +2683,7 @@ LCS/PyCommon/__init__.py -text
LCS/PyCommon/datetimeutils.py -text LCS/PyCommon/datetimeutils.py -text
LCS/PyCommon/factory.py -text LCS/PyCommon/factory.py -text
LCS/PyCommon/postgres.py -text LCS/PyCommon/postgres.py -text
LCS/PyCommon/subprocess.py -text
LCS/PyCommon/test/python-coverage.sh eol=lf LCS/PyCommon/test/python-coverage.sh eol=lf
LCS/PyCommon/test/t_dbcredentials.run eol=lf LCS/PyCommon/test/t_dbcredentials.run eol=lf
LCS/PyCommon/test/t_dbcredentials.sh eol=lf LCS/PyCommon/test/t_dbcredentials.sh eol=lf
......
...@@ -12,7 +12,8 @@ set(_py_files ...@@ -12,7 +12,8 @@ set(_py_files
methodtrigger.py methodtrigger.py
util.py util.py
postgres.py postgres.py
datetimeutils.py) datetimeutils.py
subprocess.py)
python_install(${_py_files} DESTINATION lofar/common) python_install(${_py_files} DESTINATION lofar/common)
......
from threading import Thread
try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty # python 3.x
class PipeReader:
'''
helper class to do non-blocking readline calls to a subprocess stdput or stderr pipe.
'''
def __init__(self, pipe):
self.__line_buffer = ''
self.__queue = Queue()
self.__thread = Thread(target=PipeReader.enqueue_output, args=(pipe, self.__queue))
self.__thread.daemon = True # thread dies with the program
self.__thread.start()
@staticmethod
def enqueue_output(pipe, queue):
try:
for line in iter(pipe.readline, b''):
queue.put(line)
except Exception as e:
logger.error(e)
def readline(self, timeout=None):
start = datetime.now()
while timeout==None or datetime.now() - start <= timedelta(seconds=timeout):
try:
result = self.__queue.get(True, timeout)
if result:
self.__line_buffer += result
except Empty:
pass
endline_idx = self.__line_buffer.find('\n')
if endline_idx > -1:
line = self.__line_buffer[:endline_idx]
self.__line_buffer = self.__line_buffer[endline_idx+1:]
return line
return ''
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment