如何在Python中跟蹤日志文件?我希望在Python中可以在沒有阻塞或鎖定的情況下使用tail-F或類似的輸出。我找到了一些很老的代碼這里,但我想現(xiàn)在肯定有更好的方法或圖書館來做同樣的事情了。有人聽說過嗎?理想情況下,我會有tail.getNewData()每次我想要更多數(shù)據(jù)的時候我都可以打電話給你。
3 回答

浮云間
TA貢獻(xiàn)1829條經(jīng)驗 獲得超4個贊
非阻塞
import timeimport subprocessimport select f = subprocess.Popen(['tail','-F',filename],\ stdout=subprocess.PIPE,stderr=subprocess.PIPE)p = select.poll()p.register(f.stdout)while True: if p.poll(1): print f.stdout.readline() time.sleep(1)
time.sleep(1)
print f.stdout.readline()
阻塞
import subprocess f = subprocess.Popen(['tail','-F',filename],\ stdout=subprocess.PIPE,stderr=subprocess.PIPE)while True: line = f.stdout.readline() print line
f.kill()
.
添加回答
舉報
0/150
提交
取消