bash 关闭缓冲

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8416586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:08:55  来源:igfitidea点击:

Turn off buffering

pythonlinuxbashbuffering

提问by fodon

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

下面的缓冲区在哪里......我该如何关闭它?

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

我在 python 程序中写出标准输出,如下所示:

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:

我尝试了以下方法来摆脱可能的缓冲......但没有运气:

  • 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 tail -f data.txt | stdbuf -o0 -i0 grep -e APL | stdbuf -i0 -o0 python -u Interpret.py

  • 如上所述,使用带有 python 调用的 -u 标志
  • 在每次 sys.stdout.write() 调用之后调用 sys.stdout.flush() ......所有这些都创建了一个缓冲流,python 等待一分钟来打印出前几行。
  • 使用了以下修改后的命令:

    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 

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

这会产生稳定的线条流......它肯定不像 python 命令那样缓冲。

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

那么,如何关闭缓冲?解答:事实证明,管道的两端都有缓冲。

采纳答案by unutbu

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

我认为问题在于grep缓冲其输出。当您使用管道时,它就是这样做的tail -f | grep ... | some_other_prog。要grep每行刷新一次,请使用以下--line-buffered选项:

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

APL

APL

where test.pyis:

在哪里test.py

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

(Tested on linux, gnome-terminal.)

(在 linux、gnome-terminal 上测试。)

回答by ivan_pozdeev

file.readlines()and for line in filehave internal buffering which is not affected by -uoption (see -u option note). Use

file.readlines()for line in file具有不受-u选项影响的内部缓冲(请参阅-u 选项注释)。用

while True:
   l=sys.stdin.readline()
   sys.stdout.write(l)

instead.

反而。

By the way, sys.stdoutis line-buffered by default if it points to terminal and sys.stderris unbuffered (see stdio buffering).

顺便说一下,sys.stdout如果它指向终端并且sys.stderr是无缓冲的,则默认情况下是行缓冲的(请参阅stdio 缓冲)。

回答by Anand Balachandran Pillai

The problem is in your for loop. It will wait for EOF before continuing on. You can fix it with a code like this.

问题出在您的 for 循环中。它将在继续之前等待 EOF。你可以用这样的代码修复它。

while True:
    try:
        line = sys.stdin.readline()
    except KeyboardInterrupt:
        break 

    if not line:
        break

    print line,

Try this out.

试试这个。

回答by Burhan Khalid

sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)and make sure PYTHONUNBUFFEREDis set in your environment.

sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)并确保PYTHONUNBUFFERED在您的环境中设置。