delay a task until certain time

后端 未结 8 2086
梦谈多话
梦谈多话 2021-01-11 11:59

What I want to do in a python script is sleep a number of seconds until the required time is reached. IE: if runAt setting is 15:20 and current time is 10:20, how can I wor

8条回答
  •  情深已故
    2021-01-11 12:08

    Here's a solution that uses the Arrow module:

    def wait_until(specified_dt: arrow.Arrow) -> None:
    """Stay in a loop until the specified date and time."""
    # Initially check every 10 seconds.
    refresh = 10
    current_dt = arrow.utcnow()
    
    while current_dt < specified_dt:
        # Check every millisecond if close to the specified time.
        current_dt = arrow.utcnow()
        if (specified_dt - current_dt).seconds < 11:
            refresh = .001
    
        time.sleep(refresh)
    

提交回复
热议问题