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)

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

<pedantic>libc is buffering the output, not the OS.</pedantic>
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.

1 Comment

This clarifies the problem I had. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.