5 Simple thread based asynchronous file reader for Python.
7 see https://github.com/soxofaan/asynchronousfilereader
10 Copyright (c) 2014 Stefaan Lippens
18 from Queue import Queue
21 from queue import Queue
24 class AsynchronousFileReader(threading.Thread):
26 Helper class to implement asynchronous reading of a file
27 in a separate thread. Pushes read lines on a queue to
28 be consumed in another thread.
31 def __init__(self, fd, queue=None, autostart=True):
37 threading.Thread.__init__(self)
44 The body of the tread: read lines and put them on the queue.
47 line = self._fd.readline()
54 Check whether there is no more content to expect.
56 return not self.is_alive() and self.queue.empty()
60 Get currently available lines.
62 while not self.queue.empty():
63 yield self.queue.get()