time.sleep(delay) will sleep for, at most,
delay seconds. This function will sleep for at least
Python, 12 lines
1 2 3 4 5 6 7 8 9 10 11 12
from time import time, sleep def sleep_min(delay): accum = 0 remainder = delay while accum < delay: begin = time() sleep(remainder) end = time() slept = end - begin accum += slept remainder -= slept
I've had to write this function so many times I can't count. The most recent time I wrote this was for a test that ensured that my long-polling request was indeed taking advantage of the long-polling feature in my server.
time.sleep seems pretty reliable: