Usage of sys.stdout.flush() method
Asked Answered
E

7

231

What does sys.stdout.flush() do?

Epineurium answered 4/4, 2012 at 21:22 Comment(1)
See : #231251Barren
M
234

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

Marmara answered 4/4, 2012 at 21:35 Comment(2)
@Ciastopiekarz What can we do to have the buffer flushed on windows?Hirz
@Ciastopiekarz How do you figure? If I take Andrew Clark's Python script, and replace the print line with sys.stdout.write("%d" % i), then I have to uncomment the call to sys.stdout.flush() to get the buffer to display as the script is executing.Nova
K
150

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.

Kine answered 4/4, 2012 at 21:34 Comment(2)
In Python 3.x, 'print i' should be replaced with print(i, end=' ') because print() in Python 3 has a default prefix end='\n' which prompts the console to flush.Tentacle
I am just confused, when I remove the comma it is working fine as expected. Is there any buffer logic for new lines??Krigsman
R
10

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
Raul answered 27/10, 2015 at 9:41 Comment(1)
Missing a comma after the print i to get your outputOverton
P
3

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.

Papistry answered 16/7, 2018 at 12:41 Comment(0)
P
2
import sys
for x in range(10000):
    print "HAPPY >> %s <<\r" % str(x),
    sys.stdout.flush()
Pazit answered 28/8, 2016 at 7:12 Comment(0)
S
1

You can see the differences b/w these two

import sys

for i in range(1,10 ):
    sys.stdout.write(str(i))
    sys.stdout.flush()

for i in range(1,10 ):
    print i

In the first case, the characters are output one by one after each is written, because of the flush. In the second case, the characters are buffered by Python until it thinks it's got something worth the effort to write, and then written all in a batch.

If you add, say, a time.sleep(0.2) in the loops, this becomes more obvious.

Sheley answered 10/8, 2020 at 3:52 Comment(1)
And what is the difference here? This answer appears to be incomplete (even though I obviously know the difference, but it's good practice to explain answers in full on SE and not leave things hanging).Varden
Z
0

Imagine you have a toy box where you keep all your toys. When you want to play with a toy, you usually take it out of the box, right?

In a similar way, when you write something on the computer, it gets stored in a temporary place called a buffer. Think of it like a toy box for computer data. The computer waits until the toy box is full before it takes the data out of the box and shows it to you on the screen.

However, sometimes you might want to play with a toy right away, without waiting for the toy box to fill up with other toys. This is like when you call sys.stdout.flush() in Python. It tells the computer to take the data out of the buffer and show it to you on the screen right away, without waiting for the buffer to fill up.

Zaria answered 6/2, 2023 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.