Python Forum
sleep_cycle() - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: sleep_cycle() (/thread-30534.html)



sleep_cycle() - Skaperen - Oct-24-2020

sleep_cycle.py:
from time import sleep,time as secs

def sleep_cycle(*arg):
    """Sleep to an offset time cycle.
sleep_cycle(offset,cycle) returns time slept in seconds (float)."""
    # example: sleep to 15 minutes past the hour: sleep_cycle(3600,900)
    # example: sleep to noon: sleep_cycle(86400,43200)
    if arg:
        off = int(arg[0]*1000000000)
        cyc = int(arg[1]*1000000000) if len(arg)>1 else 0
        now = int(secs()*1000000000)
        if cyc:
            off = (off-now)%cyc
        off = off/1000000000
        sleep(off)
        return off
    else:
        return 0