time.monotonic() roll over

Dave Angel davea at davea.name
Thu Dec 4 20:21:05 EST 2014


On 12/04/2014 07:39 PM, Steven D'Aprano wrote:
> 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.

And I say you have it exactly backwards.  People should NOT call sleep() 
to measure something, they should only call it to release the processor 
for "up to" a specified time.  That time should normally be a major 
fraction of the time that some external event is expected to take.  Then 
the code should check the external event and sleep some more if necessary.

If efficiency were not an issue, the code would just do a busy loop, 
checking the external condition repeatedly.

Since the OS has no way of knowing whether the thing being waited for is 
a thread, another process, a human being, a network operation, or the 
end of the world, the interpretation of sleep needs to be the most 
conservative one.  There are many ways of suspending a process, and some 
of them will also suspend the external event.  Since the OS cannot know 
which case is significant, it has to return control to the caller at the 
soonest of the many possible interpretations.


-- 
DaveA



More information about the Python-list mailing list