Skip to content
Closed
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
8 changes: 7 additions & 1 deletion streamz/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class from_textfile(Source):
start: bool (False)
Whether to start running immediately; otherwise call stream.start()
explicitly.
from_end: bool (False)
Whether to begin streaming from the end of the file (i.e., only emit
lines appended after the stream starts).

Example
-------
Expand All @@ -64,11 +67,12 @@ class from_textfile(Source):
Stream
"""
def __init__(self, f, poll_interval=0.100, delimiter='\n', start=False,
**kwargs):
from_end=False, **kwargs):
if isinstance(f, str):
f = open(f)
self.file = f
self.delimiter = delimiter
self.from_end = from_end

self.poll_interval = poll_interval
super(from_textfile, self).__init__(ensure_io_loop=True, **kwargs)
Expand All @@ -78,6 +82,8 @@ def __init__(self, f, poll_interval=0.100, delimiter='\n', start=False,

def start(self):
self.stopped = False
if self.from_end:
self.file.seek(0, 2)
self.loop.add_callback(self.do_poll)

@gen.coroutine
Expand Down
20 changes: 20 additions & 0 deletions streamz/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,26 @@ def test_from_file():
yield gen.sleep(0.01)
assert time() < start + 2 # reads within 2s

source = Stream.from_textfile(fn, poll_interval=0.010,
asynchronous=True, start=False,
from_end=True)
L = source.map(json.loads).pluck('x').sink_to_list()

source.start()

yield gen.sleep(0.10)

assert L == []

f.write('{"x": 6, "y": 2}\n')
f.write('{"x": 7, "y": 2}\n')
f.flush()

yield await_for(lambda: len(L) == 2, timeout=5)

assert L == [6, 7]



@gen_test()
def test_filenames():
Expand Down