Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions asynchronousfilereader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def __init__(self, fd, queue=None, autostart=True):
if autostart:
self.start()

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
if self.is_alive():
self.join()

def run(self):
"""
The body of the tread: read lines and put them on the queue.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_asynchronousfilereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ def test_simple(self):

self.assertEqual(['line1\n', 'line2\n'], lines)

def test_with(self):
data_stream = StringIO('line1\nline2\n')
with AsynchronousFileReader(data_stream) as reader:
lines = []
while not reader.eof():
for line in reader.readlines():
lines.append(line)

self.assertEqual(['line1\n', 'line2\n'], lines)


if __name__ == '__main__':
unittest.main()