forked from sudharsan2020/python-mastery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcofollow.py
40 lines (34 loc) · 801 Bytes
/
cofollow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# cofollow.py
import os
import time
import csv
def follow(filename,target):
with open(filename,"r") as f:
f.seek(0,os.SEEK_END)
while True:
line = f.readline()
if line != '':
target.send(line)
else:
time.sleep(0.1)
# Decorator for coroutines
from functools import wraps
def consumer(func):
@wraps(func)
def start(*args,**kwargs):
f = func(*args,**kwargs)
f.send(None)
return f
return start
# Sample coroutine
@consumer
def printer():
while True:
try:
item = yield
print(item)
except Exception as e:
print('ERROR: %r' % e)
# Example use.
if __name__ == '__main__':
follow('../../Data/stocklog.csv', printer())