Usage of sys.stdout.flush() method

Go To StackoverFlow.com

129

What does sys.stdout.flush() do?

2012-04-04 21:22
by I159
See : http://stackoverflow.com/questions/230751/how-to-flush-output-of-python-prin - nitin 2012-04-04 21:25


133

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

2012-04-04 21:35
by Haldean Brown
this does not work on windows though!! - Ciasto piekarz 2014-08-16 13:53
@Ciastopiekarz What can we do to have the buffer flushed on windows - helplessKirk 2015-11-10 16:41
@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 - Bacon Bits 2016-04-25 04:35


94

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.

2012-04-04 21:34
by Andrew Clark
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 - ofer.sheffer 2014-08-02 17:53
I was just having this problem! Thanks so much for clarifying why this solves it - Brent Pappas 2017-10-23 19:43
I am just confused, when I remove the comma it is working fine as expected. Is there any buffer logic for new lines? - Sunil Lulla 2018-06-19 13:41


5

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:~$

*****OUTPUT*****

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

chiru@online:~$

2015-10-27 09:41
by chiru
Missing a comma after the print i to get your outpu - SwimBikeRun 2019-02-28 02:19


1

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.

2018-07-16 12:41
by Sagar Dhungel


0

import sys
for x in range(10000):
    print "HAPPY >> %s <<\r" % str(x),
    sys.stdout.flush()
2016-08-28 07:12
by JieJ
Ads