1

I'm sure I am missing something very basic... I have a python script that calls a sleep function. I'd like the main thread (in this case) to sleep for 1 hour (3600 seconds).

Here is the relevant code reproduced:

import time
from datetime import datetime

now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
print("Sleep", flush=True)

time.sleep(3600)

now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
print("Done sleeping", flush=True)

The output after 2 hours is:

Current Time = 08:45:45
Sleep

But my code never exits the sleep function to print the finish time or "Done sleeping" message.

(I added the flush parameter to the print statement to eliminate potential buffering, but I don't think that's relevant here).

Any thoughts on why my code doesn't exit the sleep function after 1 hour?

Thanks.

Dave
  • 21
  • 4
  • Yes, if I do a few seconds it does work. – Dave Aug 09 '21 at 17:58
  • I'm currently testing the code via Visual Code, – Dave Aug 09 '21 at 17:58
  • Ok, because I tried 10 seconds and it was fine. – quamrana Aug 09 '21 at 17:58
  • 1
    maybe your terminal just times out? – Anshul Aug 09 '21 at 18:00
  • It worked fine for me on short time as well. Perhaps it would be better to use a loop with a shorter sleep? – bicarlsen Aug 09 '21 at 18:00
  • Which python version are you running? – user107511 Aug 09 '21 at 18:02
  • See [this](https://stackoverflow.com/questions/25355257/pythons-time-sleep-method-waits-incorrect-amount-of-time) and [that](https://stackoverflow.com/questions/37402649/python-time-sleep-indefinitely) – Anshul Aug 09 '21 at 18:02
  • 1
    In general - you want to avoid sleeping for long time periods, your software conditions might change during the sleep, and with shorter sleeping times you can check whether those have changed – user107511 Aug 09 '21 at 18:06
  • Does it work if you run the script from a terminal window instead of VSCode? – Barmar Aug 09 '21 at 18:21
  • As an update, it just finished: But at an odd time. It started at 8:45. Slept for 3600 seconds. Then finished at 11:01. Odd. – Dave Aug 09 '21 at 20:26
  • @user107511 - agreed, but this is just a snippet of the larger program. The sleep occurs on separate threads. (Note: untreated to my issue - the sleep function doesn't work in even the mosts basic script as shown). – Dave Aug 09 '21 at 20:28
  • @anshul, thank you. Re: "Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system." I might expect a second or two, but it's being delayed by hours. There is nothing else running on my system (background processes, sure, but nothing of significance). – Dave Aug 09 '21 at 20:32
  • @user107511, I'm using Python version 3.9.6 – Dave Aug 09 '21 at 20:37
  • @Barmar Trying that now. Will report back... – Dave Aug 09 '21 at 21:01

2 Answers2

0

You could try a loop like this to tell you how long it takes to time out.

import time
from time import perf_counter
from datetime import datetime

start = perf_counter()
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
print("Sleep", flush=True)

# time.sleep(3600)
for i in range(3600):
    print("Seconds sleeping:{}".format(i))
    time_elapsed = perf_counter() - start
    print("Elapsed Time =", time_elapsed)
    time.sleep(1)

now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
print("Done sleeping", flush=True)
Jortega
  • 3,616
  • 1
  • 18
  • 21
  • It's a great suggestion, but any thoughts on how this would help to debug the core issue? – Dave Aug 10 '21 at 01:24
  • I have a feeling the issue might have to do with your computer's power saving settings. You could see how many minuets it takes to fail and then change the settings and check if the time it takes to fail corresponds with the updated settings. I would try the lock screen setting first. – Jortega Aug 10 '21 at 11:27
  • very interesting, thank you. I will disable all battery settings and try again. Will report back soon... – Dave Aug 10 '21 at 15:26
  • I disabled all sleep / battery settings so my computer would not sleep (it does not) and yet it still fails. Initially I thought this was a VS Code issue - it's not. I can re-create the issue at will via the terminal Oddly, it only seems to happen with a sleep of 3600 (or more)... – Dave Aug 10 '21 at 19:47
0

I added the perf_counter that @Jortega suggested.

I ran it for 1 hour (3600) seconds, and in this case it finished, but also showed the sleep function "drifted" by about 18 seconds over the 1 hour period. Output below:

Seconds sleeping:3597
Elapsed Time = 3615.837408959
Seconds sleeping:3598
Elapsed Time = 3616.842702875
Seconds sleeping:3599
Elapsed Time = 3617.847937667
Current Time = 15:20:32
Done sleeping

This is interesting but unfortunately, I'm still not sure why the following code does not work:

import time
from datetime import datetime

now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
print("Sleep", flush=True)

time.sleep(3600)

now = datetime.now()   <================== Code never executes.
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
print("Done sleeping", flush=True)

If anyone has any suggestions, it would be appreciated.

Dave
  • 21
  • 4