Search code examples
pythonpython-3.xmultithreadingmultiprocessingapscheduler

How to execute a python function for every one hour exactly using apscheduler independent of program running start time


I have a function, which is executed for every hour using threading module. But I have encountered problems with this,

i.e., the function is not running after couple of hours, like after 10 - 12 hours (This time is varying)

def update_df():
    df = pd.read_sql(sql_query, connections['DB2'])
    threading.Timer(60*60*1, update_df).start()

update_df()

Questions:

  1. What is best practice, to implement this function such that it should run for every IST hour(not on system time)?
  2. Why threading module haven't worked properly (Is there any built-in module to do same job)?

Edit-1:

  • Question-1 is sovled
  • Question-2 need more visibility

Solution

  • Extending the answer of @Harley,

    You need to use trigger='cron' instead of 'interval' to execute every hour

    Document

    interval : This should be used when you want to run a function after fixed number of hours/minutes/seconds irrespective of day/month/year.

    cron : This should be used when you want to run a function at a particular day/month/year at a particular hour/minute/second.

    from apscheduler.schedulers.background import BackgroundScheduler
    scheduler = BackgroundScheduler()
    scheduler.configure(timezone='Asia/Kolkata')
    scheduler.add_job(method, trigger='cron', hour='*') # Execute every hour independent of execution time(i.e., 1:00:00, 2:00:00, so on..)
    scheduler.add_job(method_1, 'interval', minutes=30) # Execute on every 30 minutes depends program execution time
    scheduler.start()
    

    To stop scheduler, use .remove_all_jobs()

    Ex: scheduler.remove_all_jobs()