Python Forum

Full Version: sleep_cycle()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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