⇤ ← Revision 1 as of 2010-02-10 01:08:45
Size: 1999
Comment:
|
← Revision 2 as of 2010-02-10 03:11:53 ⇥
Size: 2176
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 72: | Line 72: |
* [[http://code.activestate.com/recipes/157035/|tail -f in Python « ActiveState Code]] * [[http://pypi.python.org/pypi/tailer/0.2.1|Python Package Index : tailer 0.2.1]] |
Contents
配合tail -f
问题
john <[email protected]> sender-time Sent at 09:14 (GMT+08:00). Current time there: 9:03 AM. ✆ reply-to [email protected] to [email protected] 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 ,采取这种方法的话,这种方法不好用(没有输出)。
请问各位,这种情况下应该怎么做呢?
原因
黄毅 <[email protected]> sender-time Sent at 09:32 (GMT+08:00). Current time there: 9:05 AM. ✆ reply-to [email protected] to python-cn <[email protected]> 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 <[email protected]> sender-time Sent at 09:03 (GMT+08:00). Current time there: 9:08 AM. ✆ reply-to [email protected] to [email protected] date Wed, Feb 10, 2010 at 09:03
反馈
创建 by -- ZoomQuiet [2010-02-10 01:08:45]