9

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.

3
  • 4
    Yes, there is an upper limit: When someone trips over the power cable of your machine ;-) Commented Dec 21, 2009 at 16:42
  • 4
    What "issue" are you having? Be specific. Commented Dec 21, 2009 at 16:48
  • My guess is that it's platform specific, but I have not enough knowledge to proove anything. Commented Dec 21, 2009 at 16:50

7 Answers 7

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for not making the same mistake @mykhal does, where the cumulative effect of sleeping in small increments will build up to result in a noticeably greater total sleep than desired.
8

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.

4 Comments

The wonder of copy&paste... (see eliben's answer). However, +1 for including the source with link.
@Boldewyn: only because my firefox froze was I 3 seconds later then eli.
@Boldewyn: we do provide different assessments, however.
Beside probable wait duration, there seems to be also a hard limit, differ in versions.
7

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)

Comments

5

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

sleep(4294967.2950000003911901) 

OverflowError: sleep length is too large

6 Comments

How did you obtain that number ?
You are a mystery sir
@Matt Interesting!
The threshold looks the same for me on Windows 10.
It’s interesting that i test on Repl.it Python compiler with my ipad, but it seems like it have bigger limit then this.
|
1

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

1 Comment

Sorry, didn't see the other answers before posting.
1

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) 

2 Comments

This is a bit better, but it still might sleep for less than n if the sleep() calls are getting interrupted early.
well, in this case, i's change the function to: t0 = time.time(); while time.time() < t0 + n: sleep(1)
0

I've also encountered a situation where i had to Question myself this. in my case my thread would need to sleep like for a long time (days, months). As matt Answered with an interesting fact that there is a limit. 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.

So anyways, after all we are limited to it but i found a solution to that which is to divide seconds into chunks with a max_sleep_secs and then looping through that chunks list and sleep.

import time def sweet_dreams(secs: float): DEBUG = False max_sleep_secs = 1000000 secs_extra_ms = 0 if secs > max_sleep_secs: if type(secs) is float: secs_extra_ms = str(secs).split('.')[-1] if secs_extra_ms: secs_extra_ms = secs_extra_ms.lstrip('0') secs_extra_ms = float(f"0.{secs_extra_ms}") secs = int(secs) secs_divide = float(secs) / max_sleep_secs # e.g: 2.4123 secs_int = int(secs_divide) # e.g: 2 secs_decimal_remainder = int(str(secs_divide).split(".")[-1]) # e.g 4123 chunks = [max_sleep_secs for _ in range(secs_int)] if secs_decimal_remainder: chunks.append(int(str(secs_decimal_remainder).lstrip('0'))) if secs_extra_ms: chunks.append(secs_extra_ms) if DEBUG: print(f"Debug [sweet_dreams]: -> Total: {secs} | Max: {max_sleep_secs} | Chunks: {chunks}") quit() for seconds in chunks: time.sleep(seconds) else: if DEBUG: print(f"Debug [sweet_dreams]: -> Total: {secs} | Max: {max_sleep_secs} | Chunks: None") quit() time.sleep(secs) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.