Start a Function at Given Time

Viewed 115322

How can I run a function in Python, at a given time?

For example:

run_it_at(func, '2012-07-17 15:50:00')

and it will run the function func at 2012-07-17 15:50:00.

I tried the sched.scheduler, but it didn't start my function.

import time as time_module
scheduler = sched.scheduler(time_module.time, time_module.sleep)
t = time_module.strptime('2012-07-17 15:50:00', '%Y-%m-%d %H:%M:%S')
t = time_module.mktime(t)
scheduler_e = scheduler.enterabs(t, 1, self.update, ())

What can I do?

9 Answers

Might be worth installing this library: https://pypi.python.org/pypi/schedule, basically helps do everything you just described. Here's an example:

import schedule
import time

def job():
    print("I'm working...")

schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

I've confirmed the code in the opening post works, just lacking scheduler.run(). Tested and it runs the scheduled event. So that is another valid answer.

>>> import sched
>>> import time as time_module
>>> def myfunc(): print("Working")
...
>>> scheduler = sched.scheduler(time_module.time, time_module.sleep)
>>> t = time_module.strptime('2020-01-11 13:36:00', '%Y-%m-%d %H:%M:%S')
>>> t = time_module.mktime(t)
>>> scheduler_e = scheduler.enterabs(t, 1, myfunc, ())
>>> scheduler.run()
Working
>>>

had a really hard time getting these answers to work how i needed it to,

but i got this working and its accurate to .01 seconds

from apscheduler.schedulers.background import BackgroundScheduler
    
sched = BackgroundScheduler()
sched.start()

def myjob():
    print('job 1 done at: ' + str(dt.now())[:-3])

dt = datetime.datetime
Future = dt.now() + datetime.timedelta(milliseconds=2000)
job = sched.add_job(myjob, 'date', run_date=Future)

tested accuracy of timing with this code: at first i did 2 second and 5 second delay, but wanted to test it with a more accurate measurement so i tried again with 2.55 second delay and 5.55 second delay

dt = datetime.datetime
Future = dt.now() + datetime.timedelta(milliseconds=2550)
Future2 = dt.now() + datetime.timedelta(milliseconds=5550)

def myjob1():
    print('job 1 done at: ' + str(dt.now())[:-3])
def myjob2():
    print('job 2 done at: ' + str(dt.now())[:-3])

print(' current time: ' + str(dt.now())[:-3])
print('  do job 1 at: ' + str(Future)[:-3] + ''' 
  do job 2 at: ''' + str(Future2)[:-3])
job = sched.add_job(myjob1, 'date', run_date=Future)
job2 = sched.add_job(myjob2, 'date', run_date=Future2)

and got these results:

 current time: 2020-12-10 19:50:44.632
  do job 1 at: 2020-12-10 19:50:47.182 
  do job 2 at: 2020-12-10 19:50:50.182
job 1 done at: 2020-12-10 19:50:47.184
job 2 done at: 2020-12-10 19:50:50.183

accurate to .002 of a second with 1 test

but i did run a lot of tests and accuracy ranged from .002 to .011

never going under the 2.55 or 5.55 second delay

#everytime you print action_now it will check your current time and tell you should be done

import datetime  
current_time = datetime.datetime.now()  
current_time.hour  

schedule = {
    '8':'prep',
    '9':'Note review',
    '10':'code',
    '11':'15 min teabreak ',
    '12':'code',
    '13':'Lunch Break',
    '14':'Test',
    '15':'Talk',
    '16':'30 min for code ',
    '17':'Free',
    '18':'Help ',
    '19':'watever',
    '20':'watever',
    '21':'watever',
    '22':'watever'
}

action_now = schedule[str(current_time.hour)]
Related