help understanding time.sleep() behavior

sessile at in-gen.net sessile at in-gen.net
Sat Mar 11 19:33:16 CET 2000


In this case under Pythonwin, you could explicitly flush
stdout after each 'print x' to get the behavior you were
expecting:

>>> import sys, time
>>> for x in range(5):
...         print x
...         sys.stdout.flush()
...         time.sleep(1)


At 07:28 PM 3/10/2000 -0500, Gordon McMillan wrote:
>Andy Fox wrote:
>> 
>> I would like my program to pause for a second within a loop, but instead
>> it pauses for n seconds, where n is the number of iterations through the
>> loop, then executes the other statements.
>> 
>> I'm running PythonWin on an NT machine.
>> 
>> Here's the (embarassingly simple) code:
>> 
>> >>> import time
>> >>> for x in range (5)
>> ... 	print x
>> ... 	time.sleep(1)
>> ...                    <-- pauses here for five seconds
>> 0                      <-- then prints the values quickly
>> 1
>> 2
>> 3
>> 4
>
>Your program does exactly what you think it does. Pythonwin 
>just buffers output in a non-smooth manner. Run it under 
>python.exe instead and you'll see.
>
>- Gordon





More information about the Python-list mailing list