擅长:python、mysql、java
<p><a href="https://stackoverflow.com/a/8588245/4279">@Pengman's idea</a>的不同实现:</p>
<pre><code>#!/usr/bin/env python
import os
import time
from threading import Thread
def follow(filename):
with open(filename) as file:
file.seek(0, os.SEEK_END) # goto EOF
while True:
for line in iter(file.readline, ''):
yield line
time.sleep(1)
def logtail(category, filename):
print category
print filename
for line in follow(filename):
print line,
log_entry(category, line)
for args in Config.items('files'):
Thread(target=logtail, args=args).start()
</code></pre>
<p>其中<code>log_entry()</code>是问题中代码的副本:</p>
^{pr2}$
<p><code>follow()</code>可以使用FS监视工具实现,请参见<a href="https://stackoverflow.com/a/1476006/4279">tail -f in python with no time.sleep</a>。在</p>