APScheduler timed task is based on a framework of Quartz Python. Providing based on the date, and a fixed time interval crontab types of tasks, and the task can be persistent.
Online Documentation: https: //apscheduler.readthedocs.io/en/latest/userguide.html
First, install APScheduler
pip install apscheduler
Second, the basic concept
APScheduler has four components:
1, Triggers triggers:
Trigger comprises scheduling logic. Each job has its own trigger for determining when the next task to run. In addition to the initial configuration, the trigger is completely stateless.
There are three built-in Trigger:
(. 1) DATE: particular point in time to trigger
(2) interval: fixed interval trigger
(3) cron: triggered periodically at a particular time
2, the task store job stores: for storing task , the task is stored in memory (the default MemoryJobStore) or database.
3, the actuator executors: Actuator is submitted to the thread pool tasks or processes running in the pool, when the task is completed, the actuator notification triggers a corresponding event scheduler.
4, the scheduler schedulers: parameters as the above three components, the scheduler operates by creating instances
to select the appropriate components according to the needs of development, the following components are different schedulers:
BlockingScheduler blocking scheduler: Only applies to running scheduler program.
BackgroundScheduler background scheduler: For non-blocking case, the scheduler will run independently in the background.
AsyncIOScheduler AsyncIO scheduler for application in the case of using AsnycIO.
GeventScheduler Gevent scheduler for use by the GEvent situation.
TornadoScheduler Tornado scheduler for Tornado build applications.
TwistedScheduler Twisted scheduler for Twisted build applications.
Scheduler QtScheduler Qt, Qt use in building applications.
Third, using procedure
1, a new scheduler schedulers
2, was added scheduled task
3, a scheduled task runs
Fourth, the use case
1, the trigger date
Specific point in time the trigger is executed only once. Parameters are as follows:
parameter | Explanation |
run_date (datetime 或 str) | Run date or time jobs |
timezone (datetime.tzinfo 或 str) | Specify the time zone |
Use examples:
from datetime import datetime from datetime import date from apscheduler.schedulers.blocking import BlockingScheduler def job(text): print(text) scheduler = BlockingScheduler() # 在 2019-8-30 运行一次 job 方法 scheduler.add_job(job, 'date', run_date=date(2019, 8, 30), args=['text1']) # 在 2019-8-30 01:00:00 运行一次 job 方法 scheduler.add_job(job, 'date', Run_date = datetime (2019,. 8, 30,. 1, 0, 0), args = [ ' text2 ' ]) # in a job run 2019-8-30 01:00:01 method scheduler.add_job (job, ' DATE ' , run_date = ' 2019-8-30 01:00:00 ' , args = [ ' text3 ' ]) scheduler.start ()
2, the trigger interval
Fixed time interval trigger. Parameters are as follows:
parameter | Explanation |
weeks (int) | Weeks interval |
days (int) | A few days interval |
hours (int) | Interval hours |
minutes (int) | Intervals ranging from minutes |
seconds (int) | How many seconds interval |
start_date (datetime 或 str) | start date |
end_date (datetime 或 str) | End Date |
timezone (datetime.tzinfo 或str) |
Use examples:
Import Time from apscheduler.schedulers.blocking Import BlockingScheduler DEF Job (text): T = The time.strftime ( ' % Y-M-% D%% H:% M:% S ' , time.localtime (the time.time () )) Print ( ' {} --- {} ' .format (text, T)) Scheduler = BlockingScheduler () # every one minute run a job method scheduler.add_job (job, ' interval The ' , =. 1 minutes, args = [ ' the jobs that job1 ' ]) # during 2019-08-29 22:15:00 to 2019-08-29 22:17:00, every 1 minute 30 seconds to run a job method scheduler.add_job (Job, ' interval The ' , =. 1 minutes, 30 seconds The =, START_DATE = ' 2019-08-29 22:15:00 ' , END_DATE = ' 2019-08-29 22:17:00 ' , args = [ ' JOB2 ' ]) scheduler.start () '' ' run results: JOB2 --- 2019-08-29 22:15:00 the jobs that job1 --- 2019-08-29 22:15:46 JOB2 2019 --- 22:16:30 -08-29 the jobs that job1 --- 2019-08-29 22:16:46 the jobs that job1 2019-08-29 22:17:46 --- ... ... the rest is omitted '' '
3, triggers cron
Periodically triggered at a specific time. Parameters are as follows:
parameter | Explanation |
year (int or str) | Year 4 digits |
month (int or str) | Months (range 1-12) |
day (int or str) | Day (range 1-31) |
week (int or str) | Weeks (range 1-53) |
day_of_week (int 或 str) | The first few days or weeks week (range 0-6 or mon, tue, wed, thu, fri, sat, sun) |
hour (int or str) | When (range 0-23) |
minute (int or str) | (Range 0-59) |
second (int or str) | Sec (range 0-59) |
start_date (datetime 或 str) | The earliest start date (included) |
end_date (datetime 或 str) | Latest end time (included) |
timezone (datetime.tzinfo 或str) | Specify the time zone |
These parameters support arithmetic expressions, the value of the format are as follows:
Use examples:
import time from apscheduler.schedulers.blocking import BlockingScheduler def job(text): t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print('{} --- {}'.format(text, t)) scheduler = BlockingScheduler() # 在每天22点,每隔 1分钟 运行一次 job 方法 scheduler.add_job(job, 'cron', hour=22, minute='*/1', args=['job1']) #At 22 and 25 minutes and 23 points per day, once a job run method scheduler.add_job (job, ' the cron ' , hour = ' 22-23 ' , minute = ' 25 ' , args = [ ' JOB2 ' ]) scheduler.start ( ) '' ' operating results: the jobs that job1 --- 2019-08-29 22:25:00 JOB2 --- 2019-08-29 22:25:00 the jobs that job1 --- 2019-08-29 22:26:00 the jobs that job1 2019-08-29 22:27:00 --- ... ... the rest is omitted '' '
4, a method of adding by decorators scheduled_job ()
There are two ways to add tasks:
(1) by calling add_job () --- Code see above 1-3
(2) by decorators scheduled_job ():
The first method is the most commonly used method. The second method is to facilitate the task declared in the application is running will not change. The add_job () method returns a apscheduler.job.Job instance, you can use this example to modify or delete the task later.
import time from apscheduler.schedulers.blocking import BlockingScheduler scheduler = BlockingScheduler() @scheduler.scheduled_job('interval', seconds=5) def job1(): t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) print('job1 --- {}'.format(t)) @scheduler.scheduled_job('cron', second='*/7') def job2(): T = The time.strftime ( ' % Y-M-% D%% H:% M:% S ' , time.localtime (the time.time ())) Print ( ' JOB2 --- {} ' .format (T )) scheduler.start () '' ' run results: JOB2 --- 2019-08-29 22:36:35 the jobs that job1 --- 2019-08-29 22:36:37 JOB2 --- 2019-08-29 22:36:42 the jobs that job1 --- 2019-08-29 22:36:42 the jobs that job1 --- 2019-08-29 22:36:47 JOB2 --- 2019-08-29 22:36:49 ... The remaining will be omitted ... '' '