My first time, so be gentle!
Using Python 3.4, on Win 7, I noted that time.sleep() hangs unexpectedly. I searched, but the related questions I found did not address my issue. The following code works as expected:
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 'starting' test, then an infinite sequence of '.starting test' lines. But if I move the 'starting' message 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()
This prints 'starting test' and then hangs forever -- but only when run as a script. If I import the module into Idle and invoke 'am_i_running()', it woks fine. Also, if I omit the 'starting test' output entirely, it works fine from the command line, too. The issue appears to be with the location of the print statement, rather than with time.sleep itself.
I'm guessing that this is a feature, rather than a bug; but I'm not understanding it. Hopefully, someone here can enlighten me.