delay a task until certain time

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

    Here is a solution. This is independent of date because the new date-time "end2" has same date as current date-time and at the same time the date-time format is not changed.

    from datetime import datetime               #this is the way to import a module named datetime...
    import time                                 #this module is used to sleep
    import pause
    
    a = (datetime.now())                        #a = '2017-07-27 00:10:00.107000' #for example
    print 'the time is'
    print a
    
    end2 = a.replace(hour = 15, minute = 20, second = 00)
    
    delta = end2 - a
    print delta                                 # prints:  5:00:00
    print delta.total_seconds()                 # prints: 114605.0
    pause.seconds(delta.total_seconds())
    
    0 讨论(0)
  • 2021-01-11 12:29

    You could instead use the pause package [ https://pypi.python.org/pypi/pause/0.1.2 ]. Taking an example from their documentation -

    import pause, datetime
    dt = datetime.datetime(2013, 6, 2, 14, 36, 34, 383752)
    pause.until(dt)
    
    0 讨论(0)
提交回复
热议问题