delay a task until certain time
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 work out how many seconds to sleep? I'm not sure how to convert 15:20 to a time and current date then deduct the actual time t开发者_JAVA百科o get the seconds.
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))
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.
You could instead use the pause
package ( https://pypi.python.org/pypi/pause ). Taking an example from their documentation -
import pause, datetime
dt = datetime.datetime(2013, 6, 2, 14, 36, 34, 383752)
pause.until(dt)
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
Instead of using the function sleep(X), you can also use to a Timer
It depends on what you're planning to do.
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)
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())
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)
精彩评论