1, the introduction of

APScheduler stands for Advanced Python Scheduler. It is a lightweight Python scheduling framework for scheduled tasks. APScheduler supports three scheduling tasks: fixed interval, fixed point in time (date), and the Crontab command in Linux. It also supports asynchronous execution and background execution of scheduling tasks.

2, installation,

The PIP package management tool is the easiest and quickest way to install APScheduler.

pip install APScheduler

3. Use steps

APScheduler is relatively simple to use. Running a scheduled task requires only the following trilogy.

Create a scheduler. Add a scheduled task (Job Stores). Run scheduling tasks.

Here is a simple example code to perform a scheduled task

#! /usr/bin/env python
# coding:utf-8

import time
from apscheduler.schedulers.blocking import BlockingScheduler
import logging

LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(filename='mypython.log', level=logging.INFO, format=LOG_FORMAT)

today = time.strftime('%Y%m%d', time.localtime(time.time()))
nowDateTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

global num
num = 0

class myAP:
    def __init__(self):
        print("hello")


def sayHello():
    global num
    num = num + 1
    print(nowDateTime+'  任务一  hello word %d' % num)

def sayHello11():
    global num
    num = num + 1
    print(nowDateTime+O (* ̄)  ̄*)o hello word %d' % num)

if __name__ == "__main__":
    # BlockingScheduler
    scheduler = BlockingScheduler()
    # scheduler.add_job(sayHello, 'cron', day_of_week='0-6', hour=19, minute=50)
    scheduler.add_job(sayHello, 'interval', seconds=2)
    scheduler.add_job(sayHello11, 'interval', seconds=3)
    scheduler.start()
Copy the code

The execution result is shown in figure

4. Basic components

APScheduler has four components: a scheduler, a Job store, a trigger, and an executor.

Schedulers are task schedulers that are assigned to the controller role. It configures job storage and actuators that can be done in the scheduler, such as adding, modifying, and removing jobs.

Triggers describes the conditions under which a scheduling task is triggered. But triggers are completely stateless.

Job stores (Job stores) is a persistent repository of tasks. By default, tasks can be stored in memory or stored in various databases. Data in tasks can be serialized and stored in persistent databases, and then loaded and deserialized from the database.

Executors handle the running of jobs, usually by submitting specified callable objects to a thread or into a city during the job. The executor notifies the scheduler when the job is complete.

4.1 Schedulers

Why I personally find APScheduler so useful. It offers seven schedulers to suit our various scenarios. For example, perform an operation in the background or asynchronously. The schedulers are:

BlockingScheduler: The scheduler runs in the main thread of the current process, i.e. blocks the current thread. BackgroundScheduler: The scheduler runs in a background thread without blocking the current thread. AsyncIOScheduler: Used in conjunction with the Asyncio module, an asynchronous framework. GeventScheduler: Programs use GEvent (a high-performance Python concurrency framework) as the IO model, in conjunction with GeventExecutor. TornadoScheduler: Tornado (a Web framework) IO model is used in the program to complete the timed wake up with ioloop. Add_timeout. TwistedScheduler: With TwistedExecutor, reactor. CallLater is used to complete scheduled wake up. QtScheduler: Your application is a Qt application that uses a QTimer to perform a scheduled wake up.

4.2 Triggers (Triggers)

The APScheduler has three built-in triggers: 1) Date Trigger Date is the most basic type of scheduling. Jobs are only executed once. It represents a specific point in time. Its parameters are as follows:

parameter instructions
Run_date (datetime or STR) The running date or time of the job
Timezone (datetime tzinfo or STR) Specify the time zone

The following is an example of a date trigger:

from datetime import datetime
from datetime import date
from apscheduler.schedulers.background import BackgroundScheduler

def job_func(text):
    print(text)

scheduler = BackgroundScheduler()
# Run the job_func method once at 2017-12-13
scheduler .add_job(job_func, 'date', run_date=date(2017, 12, 13), args=['text'])
# Run job_func at 2017-12-13 14:00:00
scheduler .add_job(job_func, 'date', run_date=datetime(2017, 12, 13, 14, 0, 0), args=['text'])
Run the job_func method once on 2017-12-13 14:00:01
scheduler .add_job(job_func, 'date', run_date='the 2017-12-13 14:00:01', args=['text'])
scheduler.start()

Copy the code
The interval triggers

Fixed time interval trigger. Interval Specifies the interval for scheduling. The parameters are as follows:

parameter instructions
weeks (int) Interval of a few weeks
days (int) A few days between
hours (int) Hours apart
minutes (int) A few minutes apart
seconds (int) How many seconds apart
The start_date (datetime or STR) Start date
End_date (datetime or STR) End date
Timezone (datetime tzinfo or STR) The time zone
Cron trigger

Triggers periodically at a specific time, compatible with the Linux crontab format. It is the most powerful trigger. Let’s start with the crON parameter:

parameter instructions
Year (int or STR) Year, four digits
The month (int or STR) Month (range 1-12)
Day (int or STR) Day (range 1-31
Week (int or STR) Week (range 1-53)
Day_of_week (int or STR) Which day or week a few weeks (range 0 to 6 or mon, tue, wed and thu, fri, sat, sun)
Hour (int or STR) Hour (range 0-23)
Minute (int or STR) Points (range 0-59)
The second (int or STR) Seconds (range 0-59)
The start_date (datetime or STR) Earliest start date (inclusive)
End_date (datetime or STR) Latest End time (inclusive)
Timezone (datetime tzinfo or STR) Specify the time zone

The following is an example of a CRon trigger:

import datetime
from apscheduler.schedulers.background import BackgroundScheduler

def job_func(text): print("Current time:", datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3])
scheduler = BackgroundScheduler() 

# Execute job_func at 00:00, 01:00, 02:00 and 03:00 every Monday and Tuesday during January-September
scheduler .add_job(job_func, 'cron', month='1-3, 7-9',day='0, tue', hour='0-3') 
scheduler.start()

Copy the code

4.3 Job Store

This component is the management of scheduled tasks. 1) There are two ways to add a job, one of which is add_job() and the other is the scheduled_job() modifier.

The difference between the two methods is that the first method returns an instance of apScheduler.job. job, which can be used to change or remove the job. The second method applies only to jobs that do not change while the application is running.

Example of the second way to add tasks:

@scheduler.scheduled_job(job_func, 'interval', minutes=2) 
def job_func(text): 
    print(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]) 
    scheduler = BackgroundScheduler() 
    scheduler.start()

Copy the code

2) Remove job There are two methods to remove a job: remove_job() and job.remove(). Remove_job () removes the job based on the JOB ID, so specify an ID when the job is created. Job.remove () is used to remove jobs

scheduler.add_job(job_func, 'interval', minutes=2, id='job_one')
scheduler.remove_job(job_one)
job = add_job(job_func, 'interval', minutes=2, id='job_one')
job.remvoe()
Copy the code

The scheduler.get_jobs() method is used to obtain a list of all jobs in the current scheduler

Modify job If you want to modify a job due to schedule changes, you can use job.modify () or modify_job() to modify the job properties. Note that the ID of the job cannot be changed.

scheduler.add_job(job_func, 'interval', minutes=2, id='job_one')
scheduler.start()
# change the trigger interval to 5 minutes
scheduler.modify_job('job_one', minutes=5)

job = scheduler.add_job(job_func, 'interval', minutes=2)
# change the trigger interval to 5 minutes
job.modify(minutes=5)
Copy the code

5) Close job By default, the scheduler closes all schedulers and job stores until all running jobs complete. If you don’t want to wait, you can set the wait option to False.

scheduler.shutdown()
scheduler.shutdown(wait=false)
Copy the code

4.4 Executor

Actuators, as their name implies, are modules that perform scheduling tasks. The two most common executors are ProcessPoolExecutor and ThreadPoolExecutor

Here is an example of code that explicitly sets up the Job Store (stored using Mongo) and executor.

from pymongo import MongoClient
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.jobstores.mongodb import MongoDBJobStore
from apscheduler.jobstores.memory import MemoryJobStore
from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor


def my_job():
    print 'hello world'
host = '127.0.0.1'
port = 27017
client = MongoClient(host, port)

jobstores = {
    'mongo': MongoDBJobStore(collection='job', database='test', client=client),
    'default': MemoryJobStore()
}
executors = {
    'default': ThreadPoolExecutor(10),
    'processpool': ProcessPoolExecutor(3)
}
job_defaults = {
    'coalesce': False,
    'max_instances': 3
}
scheduler = BlockingScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults)
scheduler.add_job(my_job, 'interval', seconds=5)

try:
    scheduler.start()
except SystemExit:
    client.close()

Copy the code