问题:sys.stdout.flush()方法的用法

怎么sys.stdout.flush()办?

What does sys.stdout.flush() do?


回答 0

Python的标准输出被缓冲(这意味着它在将标准写入之前将其收集的一些数据“写入”到标准输出中)。调用会sys.stdout.flush()强制其“刷新”缓冲区,这意味着它将把缓冲区中的所有内容都写到终端,即使通常情况下它会等待这样做。

以下是有关(非)缓冲I / O及其有用之处的一些良好信息:
http : //en.wikipedia.org/wiki/Data_buffer
缓冲与无缓冲IO

Python’s standard out is buffered (meaning that it collects some of the data “written” to standard out before it writes it to the terminal). Calling sys.stdout.flush() forces it to “flush” the buffer, meaning that it will write everything in the buffer to the terminal, even if normally it would wait before doing so.

Here’s some good information about (un)buffered I/O and why it’s useful:
http://en.wikipedia.org/wiki/Data_buffer
Buffered vs unbuffered IO


回答 1

考虑以下简单的Python脚本:

import time
import sys

for i in range(5):
    print(i),
    #sys.stdout.flush()
    time.sleep(1)

这是为了打印每秒五秒钟一个号码,你要是跑不过它,因为它是现在(取决于默认的系统缓存),你可能看不到任何输出,直到脚本完成,然后一下子你会看到0 1 2 3 4印到屏幕。

这是因为输出正在缓冲中,除非sys.stdout每次刷新后print您都不会立即看到输出。从sys.stdout.flush()行中删除注释以查看区别。

Consider the following simple Python script:

import time
import sys

for i in range(5):
    print(i),
    #sys.stdout.flush()
    time.sleep(1)

This is designed to print one number every second for five seconds, but if you run it as it is now (depending on your default system buffering) you may not see any output until the script completes, and then all at once you will see 0 1 2 3 4 printed to the screen.

This is because the output is being buffered, and unless you flush sys.stdout after each print you won’t see the output immediately. Remove the comment from the sys.stdout.flush() line to see the difference.


回答 2

根据我的理解,无论何时执行打印语句,输出都会写入缓冲区。当刷新缓冲区(清除)时,我们将在屏幕上看到输出。默认情况下,程序退出时将刷新缓冲区。但是我们也可以通过在程序中使用“ sys.stdout.flush()”语句来手动刷新缓冲区。在下面的代码中,当i的值达到5时,将刷新代码缓冲区。

您可以通过执行以下代码来理解。

chiru@online:~$ cat flush.py
import time
import sys

for i in range(10):
    print i
    if i == 5:
        print "Flushing buffer"
        sys.stdout.flush()
    time.sleep(1)

for i in range(10):
    print i,
    if i == 5:
        print "Flushing buffer"
        sys.stdout.flush()
chiru@online:~$ python flush.py 
0 1 2 3 4 5 Flushing buffer
6 7 8 9 0 1 2 3 4 5 Flushing buffer
6 7 8 9

As per my understanding, When ever we execute print statements output will be written to buffer. And we will see the output on screen when buffer get flushed(cleared). By default buffer will be flushed when program exits. BUT WE CAN ALSO FLUSH THE BUFFER MANUALLY by using “sys.stdout.flush()” statement in the program. In the below code buffer will be flushed when value of i reaches 5.

You can understand by executing the below code.

chiru@online:~$ cat flush.py
import time
import sys

for i in range(10):
    print i
    if i == 5:
        print "Flushing buffer"
        sys.stdout.flush()
    time.sleep(1)

for i in range(10):
    print i,
    if i == 5:
        print "Flushing buffer"
        sys.stdout.flush()
chiru@online:~$ python flush.py 
0 1 2 3 4 5 Flushing buffer
6 7 8 9 0 1 2 3 4 5 Flushing buffer
6 7 8 9

回答 3

import sys
for x in range(10000):
    print "HAPPY >> %s <<\r" % str(x),
    sys.stdout.flush()
import sys
for x in range(10000):
    print "HAPPY >> %s <<\r" % str(x),
    sys.stdout.flush()

回答 4

根据我的理解,sys.stdout.flush()会将缓冲到该点的所有数据推送到文件对象。使用stdout时,数据在写入终端之前先存储在缓冲存储器中(一段时间或直到内存被填满)。使用flush()会强制清空缓冲区,甚至在缓冲区没有空间之前就将其写入终端。

As per my understanding sys.stdout.flush() pushes out all the data that has been buffered to that point to a file object. While using stdout, data is stored in buffer memory (for some time or until the memory gets filled) before it gets written to terminal. Using flush() forces to empty the buffer and write to terminal even before buffer has empty space.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。