##language:zh #pragma section-numbers off ##含有章节索引导航的 ZPyUG 文章通用模板 <> ## 默许导航,请保留 <> = 配合tail -f = ##startInc == 问题 == {{{ john 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 ,采取这种方法的话,这种方法不好用(没有输出)。 请问各位,这种情况下应该怎么做呢? === 原因 === {{{ 黄毅 sender-time Sent at 09:32 (GMT+08:00). Current time there: 9:05 AM. ✆ reply-to python-cn@googlegroups.com to python-cn 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 功能 * http://www.jondy.net/blog/tail-f-in-python/ === tailer模块 === {{{ No.0023 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 }}} * [[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]] {{{ #!python import tailer for line in tailer.follow(open('mail.log')): processing(line) }}} ##endInc ---- '''反馈''' 创建 by -- ZoomQuiet [<>]