def tail(f):
f.seek(0, 2)
while True:
line = f.readline()
if not line:
time.sleep(0.1)
continue
yield line
def process_matches(matchtext):
while True:
line = (yield)
if matchtext in line:
do_something_useful() # email alert, etc.
list_of_matches = ['ERROR', 'CRITICAL']
matches = [process_matches(string_match) for string_match in list_of_matches]
for m in matches: # prime matches
m.next()
while True:
auditlog = tail( open(log_file_to_monitor) )
for line in auditlog:
for m in matches:
m.send(line)
import time
def watch(fn, words):
fp = open(fn, 'r')
while True:
new = fp.readline()
# Once all lines are read this just returns ''
# until the file changes and a new line appears
if new:
for word in words:
if word in new:
yield (word, new)
else:
time.sleep(0.5)
fn = 'test.py'
words = ['word']
for hit_word, hit_sentence in watch(fn, words):
print "Found %r in line: %r" % (hit_word, hit_sentence)
您可以使用“选择”来轮询文件中的新内容。
我用它来监视日志文件。在完整的实现中,我在一个配置文件中保存了匹配项的列表,以便它可以用于多种目的。在我的增强列表中是对regex的支持,而不是简单的“in”匹配。
最简单的方法就是不断地从文件中读取,检查新的内容并测试点击率。
如果您知道数据将以行的形式出现,那么使用
readline
的这个解决方案就可以工作。如果数据是某种类型的流,您需要一个缓冲区,大于您要查找的最大的
word
,然后先填充它。这样会更复杂一点。。。相关问题 更多 >
编程相关推荐