Files
controller-wisetopdataserver/python/biopro/util/sleep.py
T
2021-12-20 14:52:55 +08:00

30 lines
631 B
Python

from time import time, sleep
from typing import Callable
def long_sleep(second: float, interrupt: Callable[[], bool] = None) -> bool:
"""sleep for long duration with interrupt-able.
:param second: sleep duration
:param interrupt: interrupt check
:return: is interrupted
"""
if second <= 1:
sleep(second)
return interrupt is not None and interrupt()
else:
t = time()
x = t + second
while t < x:
sleep(min(1., x - t))
if interrupt is not None and interrupt():
return True
t = time()
return False