Skip to content Skip to sidebar Skip to footer

Upper Limit In Python Time.sleep()?

Is there an upper limit to how long you can specify a thread to sleep with time.sleep()? I have been having issues with sleeping my script for long periods (i.e., over 1k seconds).

Solution 1:

Others have explained why you might sleep for less than you asked for, but didn't show you how to deal with this. If you need to make sure you sleep for at least n seconds you can use code like:

fromtime import time, sleep
def trusty_sleep(n):
    start=time()
    while (time() -start< n):
        sleep(n - (time() -start))

This may sleep more than n but it will never return before sleeping at least n seconds.

Solution 2:

I suppose the longer the time the more probable situation described in the docs:

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.

Solution 3:

Actual answer, at least for my machine: 4294967.2950000003911900999... seconds.

sleep(4294967.2950000003911901)

OverflowError: sleep length is too large

Solution 4:

This changed in version 3.5:

time.sleep(*secs*): The function now sleeps at leastsecs even if the sleep is interrupted by a signal, except if the signal handler raises an exception (see PEP 475 for the rationale)

Solution 5:

According to the documentation, time.sleep accepts any non-zero number [1], as you probably know. However you are under the influence of your operating systems scheduler as well [1].

[1] http://docs.python.org/library/time.html

Post a Comment for "Upper Limit In Python Time.sleep()?"