Tom
Tom

Reputation: 19

time.sleep() function not working

I'm using time.sleep() in a program but the lines seem to be running out of order. Here is the code:

print("Line 1")
time.sleep(1)
print("Line 2")
time.sleep(1)
print("Line 3")
time.sleep(1)

And it runs like this:

(waits) Line 1 Line 2 (waits) Line 3

Instead of:

Line 1 (waits) Line 2 (waits) Line 3 (waits)

I've had this problem with a lot of programs that I use this function for, never really found a fix. Any suggestions?

Upvotes: 1

Views: 3338

Answers (1)

csl
csl

Reputation: 11368

Try running without output buffering, i.e. python -u foo.py. You can also try to call sys.stdout.flush() after every print, just to test it.

Upvotes: 4

Related Questions