Premature wakeup of time.sleep()

Steve Horsley steve.horsley at gmail.com
Mon Sep 12 17:19:55 EDT 2005


Erich Schreiber wrote:
> In the Python Library Reference the explanation of the time.sleep()
> function reads amongst others:
> 
> 
>>The actual suspension time may be less than that requested because 
>>any caught signal will terminate the sleep() following execution 
>>of that signal's catching routine. Also, the suspension time may 
>>be longer than requested by an arbitrary amount because of the 
>>scheduling of other activity in the system.
> 
> 
> I don't understand the first part of this passage with the premature
> wakeup. What signals would that be?

That would be signals from the OS, frinstance, if you type ^C 
into the python console, or kill the process from another command 
line.


> I've written a script that tries to bench the responsiveness of a
> virtual Linux server. My script sleeps for a random time and on waking
> up calculates the difference of the proposed and actual wakeup time.
> 
> The essential code fragment is
> 
>     while True:
>         ts = tDelay()
>         t1 = time.time()
>         time.sleep(ts)
>         t2 = time.time()
>         twu = str(datetime.datetime.utcfromtimestamp(t1 + ts))
>         logResults(LOGFILE, twu, ts, int((t2-t1-ts)*1000))
> 
> Whereas tDelay() returns a (logarithmically) randomly distributed real
> number in the range [0.01, 1200] which causes the process to sleep
> from 10 ms to 20 minutes.
> 
> In the logs I see a about 1% of the wake-up delays beeing negative
> from -1ms to about -20ms somewhat correlated with the duration of the
> sleep. 20 minute sleeps tend to wake-up earlier then sub-second
> sleeps. Can somebody explain this to me? 
> 

I think the sleep times are quantised to the granularity of the 
system clock, shich varies from os to os. From memory, windows 95 
has a 55mS timer, NT is less (19mS?), Linux and solaris 1mS. All 
this is from memory, and really comes from discussions I have 
seen about sleep and time in java, but I am guessing that there 
are similarities.

Steve



More information about the Python-list mailing list