def smart_delay(delay: float, last_cmd: float, remain: int=0) -> float:
"""
A "smart" delay mechanism which tries to reduce the
delay as much as possible based on the time the last
delay happened.
:param delay: delay in seconds
:param last_cmd: time of last command
:param remain: counter, skip delay unless it's zero
:return: timestamp to feed to next invocation
"""
now = time.monotonic()
if remain == 0 and last_cmd is not None and delay > 0.0:
delta = now - last_cmd
if delta < delay:
sleep = delay - delta
time.sleep(sleep)
return now
评论列表
文章目录