Python sleep until time without busy loop

Let's say I want to make an event reminder in Python. It will read a file on startup containing a list of events (meetings, birthdays, etc.), figure out which one is coming soonest, and then do something like time.sleep(1e7)

. It doesn't really work: trying to sleep for ten million seconds gives OverflowError

. Ok, no problem: we'll call time.sleep(2**22)

in a loop. It's a little less nifty, but the overhead of waking up our process every 7 weeks for no good reason should not mostly impact system performance or battery life, and all those good things everyone likes when they alert us to a call sleep

into the loop.

But! This does not work! This seems to work if you don't check what happens when you sleep. If you invoke time.sleep(60)

and immediately hibernate, and then immediately turn it on, you will find that the computer shutdown time "does not count"; the program wakes up late, even if the entire hibernation / wake up takes less than a minute. So what do you do about it? The common suggestion in this situation seems to be a busy loop: a call sleep

, and then when you wake up, check the time to see how much more sleep you can afford.

The problem with this approach is that it forces you to take very short naps or be very unreliable. If the next event passes in a few months (perhaps you are just using the event reminder to keep track of birthdays), that does not mean that we can afford to sleep for several weeks, because we do not know how long the computer will be on. It may happen that the computer goes into hibernation a few minutes after we induce sleep, and remains until the day before the event that we should remind of. Thus, we could skip this event for a few weeks, as well as any other events soon after.

So what's the safe time? One second is definitely safe; in fact, one second is probably too long. Checking every ten seconds is probably safe; if someone put their computer to sleep and only turned it on less than ten seconds before their reminder is received, I don't think they can complain too much if it comes a few seconds later. One minute is more dubious; for some things, a minute late reminder makes a little difference; for others it does. For example, if your favorite TV show is at 4:00 pm and your reminder is set to 3:59 am, then if it comes for a minute, that's bad; the user knew better that this could happen and should set a reminder at 3:58 am to compensate.

But the overhead of checking the time every minute is probably NOT insignificant. At the very least, it prevents the OS from swapping the python interpreter and giving memory to a more worthy process. Or, if it does, the extra disk traffic that is generated every minute can slow down the game the user is trying to play, causing it to crash at the ideal frame rate of 60fps.

Or perhaps none of this will happen, and I overestimate the call effects sleep

from time to time, but still feel not as stupid as it is inefficient and inefficient and just plain bad. The operating system knows how long I sleep; this work is trivial from the end. My program has no idea when the computer was in sleep mode and it doesn't need to care. There must be some way to get the OS to use its knowledge to wake me up at the right time. I'm right? Is it really so? And if so, what is it?

+3


source to share





All Articles