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.
- 4Yes, there is an upper limit: When someone trips over the power cable of your machine ;-)Boldewyn– Boldewyn2009-12-21 16:42:51 +00:00Commented Dec 21, 2009 at 16:42
- 4What "issue" are you having? Be specific.Jonathan Feinberg– Jonathan Feinberg2009-12-21 16:48:07 +00:00Commented Dec 21, 2009 at 16:48
- My guess is that it's platform specific, but I have not enough knowledge to proove anything.Boldewyn– Boldewyn2009-12-21 16:50:38 +00:00Commented Dec 21, 2009 at 16:50
7 Answers
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.
1 Comment
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
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
Actual answer, at least for my machine: 4294967.2950000003911900999... seconds.
sleep(4294967.2950000003911901) OverflowError: sleep length is too large
6 Comments
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 Comment
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) 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)