ATLAS Offline Software
Loading...
Searching...
No Matches
non_blocking_stream_reader.py
Go to the documentation of this file.
1# Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
2
3import threading
4import queue as Queue
5
6
8 """! Read an output stream without blocking.
9
10 see http://eyalarubas.com/python-subproc-nonblock.html
11 @author James Robinson <james.robinson@cern.ch>
12 """
13
14 def __init__(self, input_stream):
15 """! Set up input stream, output queue and process to transfer between them."""
16 self._stream = input_stream
17 self._queue = Queue.Queue()
18 self._running = True
19
20 self._output_thread = threading.Thread(target=self.populate_queue, args=(self._stream, self._queue))
21 self._output_thread.daemon = True
22 self._output_thread.start() # start collecting lines from the stream
23
24 def populate_queue(self, stream, queue):
25 """! Collect lines from stream and put them in queue."""
26 while self._running:
27 line = stream.readline()
28 if line:
29 queue.put(line)
30
31 def readline(self, timeout=None):
32 """! Return lines from queue."""
33 try:
34 output = self._queue.get(block=(timeout is not None), timeout=timeout).rstrip()
35 return (output, self._queue.qsize())
36 except Queue.Empty:
37 return (None, 0)
38
39 def finalise(self):
40 """! Release thread resources on finalise."""
41 self._stream.flush()
42 self._running = False
43 self._output_thread.join()
__init__(self, input_stream)
Set up input stream, output queue and process to transfer between them.
populate_queue(self, stream, queue)
Collect lines from stream and put them in queue.
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:130