Python 3.4: time.sleep () hangs unexpectedly when a print call precedes

For the first time, so be gentle!

Using Python 3.4, in Win 7, I noticed that time.sleep () hangs unexpectedly. I searched, but related questions did not concern my problem. The following code works as the Expected Result:

from time import sleep

def am_i_running():
    while True:
        print('starting test')
        sleep(0.5)
        print(".", end="")

if __name__ == '__main__':
    am_i_running()

It prints an "initial" test, and then an endless sequence of lines of ".starting test". But if I translate the message 'start' so that I have:

from time import sleep

def am_i_running():
    while True:
        sleep(0.5)
        print(".", end="")

if __name__ == '__main__':
    print('starting test')
    am_i_running()

" ", - script. Idle 'am_i_running()', . , "start test", . , .

, , ; . , - .

+4
1

. print(".", end="") , . , print; , , .

. :

import sys
sys.stdout.flush()
+2

Source: https://habr.com/ru/post/1545504/


All Articles