12-Dec-2017 0 Comments paras

If we want to run a job/function/script in python on periodically, we can simply use the Advanced Python Scheduler (APScheduler) library. It allows adding new jobs or removing old ones on the fly. The following description is from the official APScheduler site : 

Among other things, APScheduler can be used as a cross-platform, application specific replacement to platform specific schedulers, such as the cron daemon or the Windows task scheduler. Please note, however, that APScheduler is not a daemon or service itself, nor does it come with any command line tools. It is primarily meant to be run inside existing applications. That said, APScheduler does provide some building blocks for you to build a scheduler service or to run a dedicated scheduler process.
APScheduler has three built-in scheduling systems you can use:
  • Cron-style scheduling (with optional start/end times)
  • Interval-based execution (runs jobs on even intervals, with optional start/end times)
  • One-off delayed execution (runs jobs once, on a set date/time)

Here is a quick example of using apscheduler for scheduling a job on a hourly basis : 

First you need to install the apsscheduler package 

pip install apscheduler

from apscheduler.schedulers.blocking import BlockingScheduler

def run_script():
	print('do something')
scheduler = BlockingScheduler()