delay a task until certain time

后端 未结 8 2087
梦谈多话
梦谈多话 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:06

    Think you can also use the following code:

    from datetime import datetime, time
    from time import sleep
    
    def act(x):
        return x+10
    
    def wait_start(runTime, action):
        startTime = time(*(map(int, runTime.split(':'))))
        while startTime > datetime.today().time(): # you can add here any additional variable to break loop if necessary
            sleep(1)# you can change 1 sec interval to any other
        return action
    
    wait_start('15:20', lambda: act(100))
    

提交回复
热议问题