import time


def sleep_check(sleep_time, check_interval, stop=lambda: False):
    """
    sleep for an overall period of time, waking up to check for a 
    stop semaphore to be set, at a given interval
     
    Inputs:
    ======
    sleep_time :: overall time to sleep in seconds
    check_interval :: period before semaphore checking, seconds
    stop :: semaphore function, returns True when time to exit

    Returns:
    =======
    None
    """

    # get initial time when starting
    start_time = time.time()

    while True:
    
        # check for stop, if present return
        if stop():
            return None

        # sleep for check interval
        time.sleep(check_interval)

        # get current time
        curr_time = time.time()

        # exit criteria is when current time i
        if curr_time -start_time >= sleep_time:
            return None