-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutilities.py
More file actions
61 lines (45 loc) · 1.3 KB
/
utilities.py
File metadata and controls
61 lines (45 loc) · 1.3 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import argparse
from time import sleep
import threading
def ReadOneLine(thefile):
line = thefile.readline()
if not line:
return line
while line[-1] != '\n':
sleep(0.1)
line += thefile.readline()
return line
def follow(thefile, freq = 2, wait_time = 60 * 16):
wait = wait_time
sleep_time = 1 / freq
while True:
line = ReadOneLine(thefile)
if not line:
if thefile.closed or wait <= 0:
return None
wait -= sleep_time
sleep(sleep_time)
continue
wait = wait_time
yield line
class FileFollower:
def __init__(self, file_name, freq = 10):
self.thefile = open(file_name, 'r')
assert(self.thefile)
self.listeners = []
self.freq = 10
def AddListener(self, listener):
self.listeners.append(listener)
def RemoveListener(self, listener):
if listener in self.listeners:
self.listeners.remove(listener)
def start(self):
threading.Thread(target=self._thread, daemon=True).start()
# follower thread
def _thread(self):
f = follow(self.thefile, self.freq)
index = 0
for line in f:
for l in self.listeners:
l(line, index)
index += 1