delay a task until certain time

后端 未结 8 1952
梦谈多话
梦谈多话 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))
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-01-11 12:10

    Instead of using the function sleep(X), you can also use to a Timer

    It depends on what you're planning to do.

    0 讨论(0)
  • 2021-01-11 12:17

    Using both arrow and pause:

    maintenance = arrow.now()
    EndAt = maintenance.replace(hour = 17, minute = 6, second = 0)
    print(maintenance,': Maintenance in progress. Pausing until :',EndAt)
    pause.until(EndAt.naive)
    
    0 讨论(0)
  • 2021-01-11 12:18

    If you subtract one datetime object from another you get a timedelta object, which has a seconds property, so you can do:

    t1 = datetime.datetime.now()
    
    # other stuff here
    t2 = datetime.datetime.now()
    delta = t2 - t1
    if delta.seconds > WAIT:
        # do stuff
    else:
        # sleep for a bit
    

    As an aside, you might want to use cron for tasks that are supposed to run at specific times.

    0 讨论(0)
  • 2021-01-11 12:20

    Using timedelta object is the way to go. Below is the example that worked for me and can easily be adjusted to any other time:

    import datetime, time
    today = datetime.datetime.now()
    
    sleep = (datetime.datetime(today.year, today.month, today.day, 15, 20, 0) - today).seconds
    print('Waiting for ' + str(datetime.timedelta(seconds=sleep)))
    time.sleep(sleep)
    

    Take into consideration that if 15:20 has already passed, this substraction will still work and will wait till the next occurrence of 15:20, because in such situations timedelta returns a negative number of days. It's 16:15 as I'm running my code:

    print(datetime.datetime(today.year, today.month, today.day, 15, 20, 0) - today)
    >>>-1 day, 23:05:00.176033
    
    0 讨论(0)
提交回复
热议问题