3
print 'foo',
time.sleep(1)
print 'bar'

This seems to run time.sleep(1) first, then prints "foo bar" all at once.

However, printing both foo and bar on its own lines produces the expected delay between the print statements:

print 'foo'
time.sleep(1)
print 'bar'

Is there something that stacks all print statements until a new line character is received?

victorlin
  • 638
  • 7
  • 14

1 Answers1

7

print by default prints to sys.stdout and it is line-buffered. you could flush the buffer each time after a print statement

import time
import sys

print 'foo'
sys.stdout.flush()
time.sleep(1)
print 'bar

reference: sys

read also: How to flush output of Python print?

Community
  • 1
  • 1
desiato
  • 1,122
  • 1
  • 9
  • 16