2
import time

def threedot():
    time.sleep(0.5)
    print '.',
    time.sleep(0.5)
    print '.',
    time.sleep(0.5)
    print '.'

threedot()

When the above code is run, the interpreter waits 1.5 seconds and then prints '. . .'; rather than wait 0.5 seconds in-between printing '.'. Why is this? (on python 2.7.3)

Pro542
  • 23
  • 1
  • 5
  • You may also consider this http://stackoverflow.com/questions/881696/unbuffered-stdout-in-python-as-in-python-u-from-within-the-program – jamylak Jun 15 '13 at 07:47

2 Answers2

3

The output is being cached until one of several events which your code doesn't actually trigger until the final print. Flush the output (hint: sys.stdout.flush()) each time.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

stdout is usually "line-buffered", meaning libc will buffer the output (and not write it to the console) until a newline is encountered, or the stream is closed.

Either write to stderr, or manually flush the output stream each time.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328