本文介绍了关闭缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪里是按照本缓冲......?我怎么把它关掉?

Where is the buffer in this following ... and how do I turn it off?

我写出来到stdout在Python程序,像这样:

I am writing out to stdout in a python program like so:

for line in sys.stdin:
    print line

有一些缓冲的事情在这里:

There is some buffering going on here:

tail -f data.txt | grep -e APL | python -u Interpret.py

我尝试以下,以摆脱可能的缓冲......有没有运气:

I tried the following to shake off possible buffering ... with no luck:


  • 如上使用-u标志使用Python调用

  • 每个sys.stdout.write函数调用后sys.stdout.flush()()调用
    ......所有这些创建蟒蛇等待像一分钟,打印出来的前几行缓冲流。

  • 使用以下修改的命令:

  • as above using the -u flag with python invocation
  • calling sys.stdout.flush() after each sys.stdout.write() call... all of these create a buffered stream with python waiting something like a minute to print out the first few lines.
  • used the following modified command:

stdbuf -o0尾-f的data.txt | stdbuf -o0 -i0的grep -e APL | stdbuf -i0 -o0蟒蛇-u国米pret.py

stdbuf -o0 tail -f data.txt | stdbuf -o0 -i0 grep -e APL | stdbuf -i0 -o0 python -u Interpret.py

要基准我的期望,我想:

To benchmark my expectations, I tried:

tail -f data.txt | grep -e APL 

这行产生了源源不断的......它肯定不是作为缓冲的python命令。

This produces a steady flow of lines ... it surely is not as buffered as the python command.

所以,我怎么关闭缓冲?
回答:原来有在管的两端缓冲

So, how do I turn off buffering?ANSWER: It turns out there is buffering on both ends of the pipe.

推荐答案

这个问题,我相信是的grep 缓冲它的输出。它这样做,当你管尾-f | grep的... | some_other_prog 。要获得的grep 每行冲洗一次,使用 - 行缓冲选项:

The problem, I believe is in grep buffering its output. It is doing that when you pipe tail -f | grep ... | some_other_prog. To get grep to flush once per line, use the --line-buffered option:

% tail -f data.txt | grep -e APL --line-buffered | test.py
APL

APL

APL

其中, test.py 是:

import sys
for line in sys.stdin:
    print(line)

(测试在Linux上,GNOME终端)。

(Tested on linux, gnome-terminal.)

这篇关于关闭缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 09:16