Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). This issue has appeared on both Windows and Unix platforms.

like image 835
Shaddi Avatar asked Dec 21 '09 16:12

Shaddi


People also ask

Is there a limit for time sleep python?

in his and my system the limit is 4.29Million secs + and different System has different limits as Someone In mats answer's comment said Repl.it has bigger limit.

What does time sleep () do in python?

Python time sleep function is used to add delay in the execution of a program. We can use python sleep function to halt the execution of the program for given time in seconds. Notice that python time sleep function actually stops the execution of current thread only, not the whole program.

How do you sleep 5 seconds in python?

sleep() Python has built-in support for putting your program to sleep. The time module has a function sleep() that you can use to suspend execution of the calling thread for however many seconds you specify.

How do you sleep for 10 minutes in python?

“python sleep 10 minutes” Code Answer print("This prints once a minute.") time. sleep(60) # Delay for 1 minute (60 seconds).


6 Answers

This changed in version 3.5:

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

like image 89
Mat Avatar answered Oct 12 '22 04:10

Mat


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:

from time 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.

like image 26
samtregar Avatar answered Oct 12 '22 02:10

samtregar


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.

like image 44
SilentGhost Avatar answered Oct 12 '22 02:10

SilentGhost


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

sleep(4294967.2950000003911901)

OverflowError: sleep length is too large

like image 44
Matt Avatar answered Oct 12 '22 03:10

Matt


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

like image 27
zpon Avatar answered Oct 12 '22 03:10

zpon


you can prevent possible issues by putting the sleep with short delay into the loop:

def sleep(n):
    for i in xrange(n):
        time.sleep(1)
like image 42
mykhal Avatar answered Oct 12 '22 02:10

mykhal