Skip to content

Commit b6d10cc

Browse files
committed
Speed up experimental worker
- Removed inline sleep call, which slowed down passes n*0.1 seconds, where n is the number of files being tailed - Inline methods that update data structures which should speed up larger installations - Make self.active() an attribute lookup instead of a method call
1 parent ba9dda3 commit b6d10cc

2 files changed

Lines changed: 20 additions & 19 deletions

File tree

beaver/worker/tail.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Tail(BaseLog):
2020
def __init__(self, filename, callback, position="end", logger=None, beaver_config=None, file_config=None):
2121
super(Tail, self).__init__(logger=logger)
2222

23-
self._active = False
23+
self.active = False
2424
self._callback = callback
2525
self._fid = None
2626
self._file = None
@@ -69,22 +69,18 @@ def open(self, encoding=None):
6969

7070
def close(self):
7171
"""Closes all currently open file pointers"""
72-
self._active = False
72+
self.active = False
7373
if self._file:
7474
self._file.close()
7575

76-
def active(self):
77-
return self._active
78-
7976
def run(self, once=False):
80-
while self._active:
81-
current_time = int(time.time())
82-
self._run_pass(current_time=current_time)
83-
self._log_debug('Iteration took {0:.6f}'.format(time.time() - current_time))
84-
time.sleep(0.1)
77+
while self.active:
78+
current_time = time.time()
79+
self._run_pass()
8580

8681
self._ensure_file_is_good(current_time=current_time)
8782

83+
self._log_debug('Iteration took {0:.6f}'.format(time.time() - current_time))
8884
if once:
8985
break
9086

@@ -99,7 +95,7 @@ def _ensure_file_is_good(self, current_time):
9995
if self._last_file_mapping_update and current_time - self._last_file_mapping_update <= self._stat_interval:
10096
return
10197

102-
self._last_file_mapping_update = int(time.time())
98+
self._last_file_mapping_update = time.time()
10399

104100
try:
105101
st = os.stat(self._filename)
@@ -128,15 +124,15 @@ def _ensure_file_is_good(self, current_time):
128124
self._update_file(seek_to_end=False)
129125
self._file.seek(position, os.SEEK_SET)
130126

131-
def _run_pass(self, current_time):
127+
def _run_pass(self):
132128
"""Read lines from a file and performs a callback against them"""
133129
line_count = 0
134130
while True:
135131
try:
136132
lines = self._file.readlines(4096)
137133
except IOError, e:
138134
if e.errno == errno.ESTALE:
139-
self._active = False
135+
self.active = False
140136
return False
141137

142138
if not lines:
@@ -321,7 +317,7 @@ def _update_file(self, seek_to_end=True):
321317
except IOError:
322318
pass
323319
else:
324-
self._active = True
320+
self.active = True
325321
try:
326322
st = os.stat(self._filename)
327323
except EnvironmentError, err:

beaver/worker/tail_manager.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def watch(self, paths=[]):
4949

5050
self._tails[tail.fid()] = tail
5151

52-
def run(self):
52+
def run(self, interval=0.1,):
5353
while self._active:
5454
for fid in self._tails.keys():
5555
if not (self._proc and self._proc.is_alive()):
@@ -63,9 +63,11 @@ def run(self):
6363

6464
self._tails[fid].run(once=True)
6565

66-
if not self._tails[fid].active():
66+
if not self._tails[fid].active:
6767
del self._tails[fid]
6868

69+
time.sleep(interval)
70+
6971
def update_files(self):
7072
"""Ensures all files are properly loaded.
7173
Detects new files, file removals, file rotation, and truncation.
@@ -80,14 +82,16 @@ def update_files(self):
8082
possible_files = []
8183
files = []
8284
if len(self._beaver_config.get('globs')) > 0:
85+
extend_files = files.extend
8386
for name, exclude in self._beaver_config.get('globs').items():
8487
globbed = [os.path.realpath(filename) for filename in eglob(name, exclude)]
85-
files.extend(globbed)
8688
self._file_config.addglob(name, globbed)
89+
extend_files(globbed)
8790
self._callback(("addglob", (name, globbed)))
8891
else:
92+
append_files = files.append
8993
for name in self.listdir():
90-
files.append(os.path.realpath(os.path.join(self._folder, name)))
94+
append_files(os.path.realpath(os.path.join(self._folder, name)))
9195

9296
for absname in files:
9397
try:
@@ -98,8 +102,9 @@ def update_files(self):
98102
else:
99103
if not stat.S_ISREG(st.st_mode):
100104
continue
105+
append_possible_files = possible_files.append
101106
fid = self.get_file_id(st)
102-
possible_files.append((fid, absname))
107+
append_possible_files((fid, absname))
103108

104109
# add new ones
105110
new_files = [fname for fid, fname in possible_files if fid not in self._tails]

0 commit comments

Comments
 (0)