下面的缓冲区在哪里。。。我该怎么关掉它?
我在一个python程序中向stdout输出如下:
for line in sys.stdin:
print line
这里有一些缓冲:
tail -f data.txt | grep -e APL | python -u Interpret.py
我试着摆脱可能的缓冲。。。运气不好:
使用了以下修改后的命令:
stdbuf-o0 tail-f data.txt | stdbuf-o0-i0 grep-e APL | stdbuf-i0-o0 python-u Interpret.py
为了衡量我的期望,我试着:
tail -f data.txt | grep -e APL
这会产生稳定的管线流。。。它肯定没有python命令那样缓冲。
那么,如何关闭缓冲? 答:原来管子的两端都有缓冲物。
问题在你的for循环中。它将等待EOF,然后继续。你可以用这样的代码来修复它。
试试这个。
我认为问题在于缓冲它的输出。当你用管道
tail -f | grep ... | some_other_prog
时,它就是这样做的。要使grep
每行刷新一次,请使用--line-buffered
选项:其中
test.py
是:(在linux上测试,gnome终端。)
file.readlines()
和for line in file
具有不受-u
选项影响的内部缓冲(请参见-u option note)。使用相反。
顺便说一下,如果
sys.stdout
指向终端并且sys.stderr
没有缓冲,则默认情况下sys.stdout
是行缓冲的(请参见stdio buffering)。相关问题 更多 >
编程相关推荐