Reputation: 839
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:
threading
module haven't worked properly (Is there any built-in module to do same job)?Edit-1:
Upvotes: 1
Views: 3017
Reputation: 5785
Extending the answer of @Harley,
You need to use trigger='cron'
instead of 'interval'
to execute every hour
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()
Upvotes: 5
Reputation: 101
you can use python apscheduler.
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
scheduler.configure(timezone='subcontinent/city')
scheduler.add_job(method-1, 'interval', seconds=10)
scheduler.add_job(method-2, 'interval', minutes=30)
scheduler.add_job(method-3, 'interval', hours=1)
//the below shceduler run mon-thu and sun on every o,15,30 and 45 minutes of each hour
scheduler.add_job(method, 'cron', minute='0,15,30,45', day_of_week='mon-thu,sun')
scheduler.start()
Upvotes: 1