time.monotonic() roll over

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Dec 4 19:39:02 EST 2014


Marko Rauhamaa wrote:

> So, if I call
> 
> time.sleep(86400)
> 
> and the program is suspended for 24 hours, should time.sleep() return
> right after it is resumed or after another 24 hours?

If the program is suspended, then no time should pass for that program.
Since sleep() is given in terms of a duration, not an absolute time ("sleep
until now + 24 hours"), if no time passes for that program, sleep() should
sleep for 24 hours *after the program resumes*.

Unfortunately a lot of systems get that wrong. E.g. I just ran "sleep 30"
from my Linux shell, immediately paused it using Ctrl-Z, waited a couple of
minutes, and used fg to continue. It returned immediately.

Why is this behaviour wrong?

Consider why people might call sleep. They're normally calling it to wait
for something else to complete. Often that something else is an external
process ("wait a few seconds to let the hard drive finish syncing, wait for
the web server to get less busy, wait for the user to catch up...") but it
might be another thread in the same process. If the process is suspended,
that other thread won't get to run, and so even though time on the outside
has continued to move forward, from the perspective of the process, it
hasn't.

The same applies even more so if the process is running in a virtual machine
which has just been suspended.

The same applies if the system clock is adjusted mid-sleep. If I call sleep
60, then immediately adjust the clock forward an hour (say, due to a
daylight savings adjustment), that shouldn't cause sleep to return. It
should still suspend for 60 seconds.



-- 
Steven




More information about the Python-list mailing list