The Apscheduler is the Advanced Python Scheduler, a Lightweight Python task scheduling framework. It allows you to schedule recurring tasks like Cron, and supports Python functions or any callable object.
Installing Apscheduler
You can use the PIP tool to install:
Pip Install Apscheduler
or download the latest source package (https://pypi.python.org/pypi/APScheduler/) on PyPI to install:
Python setup.py Install
Simple Application (translated from official documents)
Start Scheduler with default configuration
from Import Scheduler = Scheduler () Sched.start ()
fromapscheduler.scheduler importScheduler
sched =Scheduler()
sched.start()
1. Fixed-time scheduling:
fromDatetimeImportDate fromApscheduler.schedulerImportScheduler#Start SchedulerSched =Scheduler () Sched.start ( )#define functions that are automatically dispatcheddefmy_job (text):Printtext#define the execution time of a task (May 17, 2013)Exec_date = Date (2013, 5, 17) #add to the task queue and assign it to variables to make it easier to cancel such operationsJob = Sched.add_date_job (My_job, Exec_date, ['text'])
2. Cycle tasks:
def job_function (): Print " Hello World " # Job_function will be executed once every two hours sched.add_interval_job (job_function, hours=2) # Same as the above tasks, but only after 2013-5-17 18:30 sched.add_interval_job (job_function, hours=2, start_date=' 2013-5-17 18:30 ')
Adorner version:
@sched. Interval_schedule (hours=2)def job_function (): print" Hello World "
Scheduling of 3.Cron-style tasks:
def job_function (): Print " Hello World " # The job_function function will be executed at 0, 1, 2 and 3 in the third Friday in June, July, November, and December, respectively , Sched.add_cron_job (job_function, month=' 6-8,11-12', day='3rd Fri', hour='0-3 ')
Adorner version:
@sched. Cron_schedule (day=' lastSun')def Some_decorated_task (): Print " I am printed at 00:00:00 in the last Sunday of every month! "
More powerful methods of use can be further referenced in official documentation.
Apscheduler--python Cron