Contents
配合tail -f
问题
john <john.about@gmail.com> sender-time Sent at 09:14 (GMT+08:00). Current time there: 9:03 AM. ✆ reply-to python-cn@googlegroups.com to python-cn@googlegroups.com date Tue, Feb 9, 2010 at 09:14 subject [CPyUG] 关于 tail -f | python 的问题
我想实时监控系统日志(比方说mail.log),打算采取将 tail 的结果通过管道传给 a.py 来分析。
- 如果是 tail mail.log | python a.py 这种方式没有问题。
print sys.stdin.read()
- 但如果 tail -f mail.log | python a.py ,采取这种方法的话,这种方法不好用(没有输出)。
请问各位,这种情况下应该怎么做呢?
原因
黄毅 <yi.codeplayer@gmail.com> sender-time Sent at 09:32 (GMT+08:00). Current time there: 9:05 AM. ✆ reply-to python-cn@googlegroups.com to python-cn <python-cn@googlegroups.com> date Tue, Feb 9, 2010 at 09:32
你应该 readline ,read 会一直读到 EOF 才返回的,所以一直阻塞了。
对策
raw_input()
黄毅 ✆
~> cat temp.py while True: print raw_input() ~> tail -f data.txt | python temp.py & [2] 21980 ~> aaa bbb ccc ddd ddd eee ~> echo fff >> data.txt ~> fff
py 版本的tail
python实现的类似 tail -f 功能
tailer模块
No.0023 <no.0023@gmail.com> sender-time Sent at 09:03 (GMT+08:00). Current time there: 9:08 AM. ✆ reply-to python-cn@googlegroups.com to python-cn@googlegroups.com date Wed, Feb 10, 2010 at 09:03
Toggle line numbers
1 import tailer
2 for line in tailer.follow(open('mail.log')):
3 processing(line)
反馈
创建 by -- ZoomQuiet [2010-02-10 01:08:45]