last day (14 days later) » 

7:08 AM
0
A: Pause for an amount of time between iterations of a loop in Python

thefourtheyeprint, by default, prints to sys.stdout and that buffers the output to be printed, internally. Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed. Changed in version 3.3: Added the flush keyword argument. ...

 
@vks No it is not. I am able to reproduce the problem. Don't use the interactive shell, which is line-buffered, try writing the code in a file and execute it with the interpreter.
 
vks
Why do you think it is not getting flushed?
 
@vks Because the interactive shell is line buffered, so you couldn't see the difference. It prints immediately.
 
vks
The problem OP was facing was a long delay ...i dont think that 5 secs delay is realted to flushing
 
@vks That is exactly the point. The code in loop runs 10 times, and all the data written to stdout are buffered. So nothing gets printed for 5 seconds (10 * 0.5 seconds) and finally when the program exits, stdout is flushed.
 
vks
7:08 AM
It is line buffered.So every print is a line.Why will it get buffered?
yes
 
Try this program
for i in range(10):
    print(i, end='')
    time.sleep(.5)
Wait, even this buffers the output.
 
vks
i have tried with range(100)...it doesnt get buffered
using python 2.7
 
In Python 2.7, stdout doesn't buffer
OP is actually using Python 3.x
 
vks
ok den it might be the case...dont have python 3....so couldnt reproduce here
 
Open a Python 3.x Interactive shell, and try the program I have given above and you will see the difference
Oh too bad
 
vks
7:15 AM
cleaned up my coments... thanx 4 d info............ :)
 
You are welcome, I am extending my answer.
 
vks
cool :)
 
 
2 hours later…
9:13 AM
@vks I updated the answer, please check if it makes sense.
 
vks
9:30 AM
I already read that and upvoted that too :)
thanx a lot :)
 

last day (14 days later) »