Python sleep() inhibit print with comma?
Question:
If I want to use the print with the comma (2.6), it works fine as long as you don’t use time.sleep().
If you use the print with the comma, and then invoke sleep; the string will never print, if you are in a loop.
Example:
a=1
b=10
while a<b:
print "yes",
a=a+1
This works, you will see yes printed on the same line for 10 times.
But this won’t work.
a=1
b=10
while a<b:
print "yes",
time.sleep(1)
a=a+1
The expectation is that there will be a yes printed; then there is a second of wait, and then the next yes will be printed. Instead, you will see a string of 10 yes printed after 10 seconds.
Same goes if you use while loop; as long as the loop is running, and you have a sleep statement, the string won’t print until the end.
To make it work, remove the comma.
This makes impossible to print a string on the same line, if you want to specify how long you want to wait between each string.
Is this a bug in the print function?
Answers:
print
does not automatically flush your output. You need to manually do it. This can be done by using sys
module’s stdout.flush
import sys,time
a=1
b=10
while a<b:
print "yes",
sys.stdout.flush()
time.sleep(1)
a=a+1
Here is another way to prevent Python from using buffered stdio pipes (Thanks to abarnert)
Execute your program by using the -u
option as in
python -u filename.py
From the man pages.
-u
Force stdin
, stdout
and stderr
to be totally unbuffered.
One final way from Python3.3 is to use the flush
argument to the print
function
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.
If I want to use the print with the comma (2.6), it works fine as long as you don’t use time.sleep().
If you use the print with the comma, and then invoke sleep; the string will never print, if you are in a loop.
Example:
a=1
b=10
while a<b:
print "yes",
a=a+1
This works, you will see yes printed on the same line for 10 times.
But this won’t work.
a=1
b=10
while a<b:
print "yes",
time.sleep(1)
a=a+1
The expectation is that there will be a yes printed; then there is a second of wait, and then the next yes will be printed. Instead, you will see a string of 10 yes printed after 10 seconds.
Same goes if you use while loop; as long as the loop is running, and you have a sleep statement, the string won’t print until the end.
To make it work, remove the comma.
This makes impossible to print a string on the same line, if you want to specify how long you want to wait between each string.
Is this a bug in the print function?
print
does not automatically flush your output. You need to manually do it. This can be done by using sys
module’s stdout.flush
import sys,time
a=1
b=10
while a<b:
print "yes",
sys.stdout.flush()
time.sleep(1)
a=a+1
Here is another way to prevent Python from using buffered stdio pipes (Thanks to abarnert)
Execute your program by using the -u
option as in
python -u filename.py
From the man pages.
-u
Forcestdin
,stdout
andstderr
to be totally unbuffered.
One final way from Python3.3 is to use the flush
argument to the print
function
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.