I gave a general introduction to APScheduler
library , But there are some module interfaces that need extra attention , This article is rather dry , When it comes to real development , It can be used as a tool
event
Mainly APScheduler
Event class triggered in , We can go through add_listener()
Bind the listener function for the dispatcher , Do some custom operations after receiving the specified event
Different events are based on different classes , Therefore, the information available is inconsistent
but event
There is a public field in code
( Enumerated values ), So you can go through code
To identify the specific event , Then get some specific field information
It is mainly divided into 4 Classes , Let's see 4 A unique field under a class :
alias
Add or remove the alias of the task storage or execution program ( If there is )job_id
Identification of the task IDjobstore
Alias of the task store that contains the related task job_id
Identification of the task IDjobstore
Alias of the task store that contains the related task scheduled_run_times
List of times when the task is scheduled to run job_id
Identification of the task IDjobstore
Alias of the task store that contains the related task scheduled_run_time
The time when the task is scheduled to run retval
The return value of the successfully executed task exception
Exception caused by task running traceback
Where the task went wrong Job
Contains many settings , It contains triggers( trigger )
, executor( actuator )
Information about , There are also some scheduling related configuration information , For example, the maximum number of instances allowed to run , Maximum time allowed to delay execution , Whether to merge, etc , The difficulty lies mainly in creating , But the official does not want users to instantiate directly through this class Job
, But through the scheduler scheduler
Medium add_job()
To achieve , Here is a brief introduction Job
Save some information , And about operation Job
The interface of , It is convenient for us to read its source code later
Instantiate the task
scheduler
The scheduler that needs to be bound id(str)
Unique identifier of the task name(str)
The name of the task func
The function that the task actually executes args(tuple|list)
Function may need to pass in arguments kwargs(dist)
Some other callable keyword parameters , For example, describe the relevant fields of the trigger, and so on coalesce(bool)
Whether to run the task only once when multiple run times expire trigger
Trigger Object executor(str)
Actuator name misfire_grace_time(int)
The maximum time this task is allowed to delay execution ( Unit second )max_instances(int)
The maximum number of concurrent instances allowed for this task next_run_time(datetime.datetime)
The next time this task is scheduled to run Return type
Job Example Modify the configuration of this task , And save it in the associated task store , The acceptable keyword parameters are the same as those for creating this class
Return type
Job Example Pause the execution of the task
Cancel the task , And delete it from its associated task store
Return type
Job Example Switch triggers on this task
Return type
Job Example If previously suspended , Resume the task's schedule
If the referenced task is still waiting to be added to its specified task store , Then return to True.
Triggers are worth a detailed introduction , The main triggering methods are 4 Kind of
date
A one-time task that runs at a specified time inteval
Recurring tasks that run at intervals cron
Timing task combining
Combine tasks date
Is to perform a one-time task , All its configuration is simple
run_date
Time of task execution timezone
Time zone of time from datetime import date
from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()
def my_job(text):
print(text)
# You can specify the exact time when the task should run :
sched.add_job(my_job, 'date', run_date=datetime(2020, 12, 16, 16, 30, 5), args=['text'])
# The run date can also be given as text
sched.add_job(my_job, 'date', run_date='2020-12-16 16:30:05', args=['text'])
# To add a task to run immediately
sched.add_job(my_job, args=['text'])
sched.start()
inteval
It is a task to configure the cycle execution of interval running , It can be controlled by the following time
days(int)
Waiting days weeks(int)
How many weeks to wait hours(int)
Waiting hours minutes(int)
Minutes to wait seconds(int)
The number of seconds to wait start_date(datetime|str)
Trigger start date / Time ( contain )end_date(datetime|str)
The latest date that may be triggered / Time ( contain )timezone(datetime.tzinfo|str)
For date / Time zone for time calculation ( The default is the dispatcher's time zone )jitter(int|None)
The maximum random time to advance or delay the execution of a task ( Unit second )Generally, for example, every task 20 Once per minute , I can write this as
from apscheduler.schedulers.blocking import BlockingScheduler
def interval_task(text):
print(text)
scheduler = BlockingScheduler()
scheduler.add_job(func=interval_task, trigger='interval', minutes=20, args=['text'])
scheduler.start()
Usually write a simple monitoring program , Monitor memory network, etc ,inteval
Is a very simple choice
corn
Can be said to be the most powerful , The plan that can be realized is also the most comprehensive , The following parameters are mainly configured
year(int|str)
4 Number of years month(int|str)
month (1-12 perhaps ‘jan’, ‘feb’, ‘mar’, ‘apr’, ‘may’, ‘jun’, ‘jul’, ‘aug’, ‘sep’, ‘oct’, ‘nov’, ‘dec’)day(int|str)
(1-31) God week(int|str)
ISO Zhou (1-53)day_of_week(int|str)
Number or name of the working day (0-6 or mon,tue,wed,thu,fri,sat,sun)hour(int|str)
Hours (0-23)minute(int|str)
minute (0-59)second(int|str)
second (0-59)start_date(datetime|str)
Trigger start date / Time ( contain )end_date(datetime|str)
The latest date that may be triggered / Time ( contain )timezone(datetime.tzinfo|str)
For date / Time zone for time calculation ( The default is the dispatcher's time zone )jitter(int|None)
The maximum random time to advance or delay the execution of a task ( Unit second ) among year
, month
, day
, week
, day_of_week
, hour
, minute
, second
These fields support expression configuration
Now the default supported expression format is as follows :
1st
, 2nd
, 3rd
, 4th
, 5th
, last
)last x At the last of the month Zhou x Trigger last Trigger on the last day of the month x,y,z You can separate multiple expressions with commas Look at the description to see these expressions , Not all the fields mentioned above are satisfied , Let's not expand here , Then there will be a summary of the source code , Will introduce this knowledge
This trigger combines the behavior of other triggers in different ways , Generate plans that are more complex than any single trigger
AndTrigger
It can be agreed that all given triggers are always returned Earliest next trigger time . When any given trigger completes its plan , The trigger is considered completed .OrTrigger
Always return by any given trigger The earliest next trigger time generated . When all the given triggers have completed their schedules , The trigger will be considered completed .Their parameters are the same
triggers(list)
Triggers that need to be combined jitter (int|None)
A random number Every time 2 Run once an hour , But only on Saturdays and Sundays :job_function
from apscheduler.triggers.combining import AndTrigger
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.triggers.cron import CronTrigger
trigger = AndTrigger([IntervalTrigger(hours=2),
CronTrigger(day_of_week='sat,sun')])
scheduler.add_job(job_function, trigger)
Every Monday morning 2 Point run , Every Tuesday afternoon 3 Point run :job_function
trigger = OrTrigger([CronTrigger(day_of_week='mon', hour=2),
CronTrigger(day_of_week='tue', hour=15)])
scheduler.add_job(job_function, trigger)
Task storage is mainly a library for storing tasks , and BaseJobStore
Is the base class of the following classes , Except for initialization, there is a little difference , The interfaces provided are basically consistent , Here we mainly introduce the base class
MemoryJobStore
MongoDBJobStore
RedisJobStore
RethinkDBJobStore
SQLAlchemyJobStore
ZooKeeperJobStore
job(Job)
Tasks to be added ConflictingIdError
Identifier of the task job_id
Throw an exception when there is the same Add a task to the task store
Return type
list[Job]Returns a list of all tasks in this task store , Return the task list to the next run time ( Ascending ) Sort . Suspended tasks (next_run_time == None) At the end of the line
now
(datetime.datetime) Current date time Return type
list[Job] Returns before or equal to next_run_time
Task list for , The returned task must press a run time ( Ascending ) Sort
Return type
datetime.datetimeReturns the earliest running time of all tasks stored in this task store or None( If there are no active tasks )
job_id(str|unicode)
Identifier of the task Return type
JobReturn to a specific task , perhaps None ( Can't find )
Delete all tasks from the task store
job_id(str|unicode)
Identifier of the task JobLookupError
If the task does not exist Delete the specified task from the task store
Release all resources on the task store
scheduler
The scheduler that manages this task store alias
Alias for this task store job
Tasks to update JobLookupError
If the task does not exist Replace the task in the task store with the given newer version
The executor will not be directly used in the process of writing code , Because we set the actuator parameters in the configuration schedulers
When , It came in , When the next task needs to be executed ,schedulers
The actuator will be arranged to execute according to the configuration
BaseExecutor
Is the base class of the following classes , Interfaces are mainly defined in this base class , Subclasses are responsible for their own implementation
AsyncIOExecutor
DebugExecutor
GeventExecutor
ThreadPoolExecutor
ProcessPoolExecutor
TwistedExecutor
wait(bool)
Set to True
Then wait until all the tasks being executed in the actuator end and stop Stop this actuator
scheduler(apscheduler.schedulers.base.BaseScheduler)
The scheduler that manages this actuator alias(str|unicode)
Alias for this actuator When the scheduler is starting or adding an actuator to a scheduler that is already running .
Simply put, it's in schedulers
After it has been started , If we need to add a new actuator , Manual call required start
To initialize the actuator
job(Job)
The work to be performed run_times(list[datetime])
A datetime list that specifies when the task should run MaxInstancesReachedError
The exception thrown when the maximum number of instances that this task can run simultaneously is reached Submit a task to perform
The scheduler orchestrates the task storage , Task triggers and executors , and BaseScheduler
Is the base class of the following classes , All this mainly introduces the interface functions provided by the base class
BackgroundScheduler
BlockingScheduler
AsyncIOScheduler
GeventScheduler
TornadoScheduler
TwistedScheduler
Initialize the scheduler , Many configuration parameters are involved
logger (str|logging.Logger)
Log output timezone (str|datetime.tzinfo)
Default time zone ( The default is the local time zone )jobstore_retry_interval (int|float)
from jobstores
After the fetching task fails , The interval between retries job_defaults (dict)
max_instances
: The largest instance of the same task that is allowed to run at the same time , The default is 1, For example, a time-consuming 10 Minute task , however 5 Once per minute , If the default value is 1, 0 Start in minutes 1 An example , that 5 You won't start a new instance in minutes , Because there are already 1 Instances are executing coalesce
: When the same task is triggered at the same time for some reason , Whether to merge tasks into one task , Default False
misfire_grace_time
: When the error between the actual execution time and the planned execution time , That is, within the set error range , The task is called to , The task will still be performed , Otherwise, the task status will be changed to EVENTJOBMISSED
, Don't execute jobstores (dict)
Set task store information , The default storage is MemoryJobStore
executors (dict)
Set actuator information , By default ThreadPoolExecutor
, That is, the thread pool , Thread pool threads 10
SchedulerAlreadyRunningError
– If the scheduler is already running start Start the configured executor and task store , And start working on the planned tasks .SchedulerAlreadyRunningError
– If the scheduler is already running RuntimeError
– If the thread is disabled uWSGI Run under shutdown Close the scheduler and its executors and task stores .SchedulerNotRunningError
– If the scheduler has not been started pause Pause task processing in the scheduler .resume Resume task processing in the scheduler .wakeup Notifies the scheduler that there may be tasks to execute .ValueError
– If there is already an executable with the given alias remove_executor Remove the executor with the given alias from this scheduler .ValueError
– If a task store with the given alias already exists remove_jobstore Delete the task store from this scheduler by the given alias .Job
Configuration parameters and the previous Job
The introduction in is similar , BaseScheduler
Just to Job
The interface of is repackaged
But it also implements how many tasks are added to the task store , How the task is assigned to the executor, etc , So in Job
That part mentioned , The official does not want to be instantiated by users Job
scheduled_job
Decorator to dynamically decorate Job
The actual function of modify_job Modify the properties of a single task .reschedule_job Construct a new trigger for the task , And update its next run time .pause_job Make the given task not executed before explicit recovery .resume_job Resume the schedule for a given task , If the plan has been completed , Delete it .get_jobs Returns a suspended task from a specific task store or from all tasks get_job Return and give job_id
Matching Job
remove_job Delete task , Make it inoperable .JobLookupError
– If the task is not found remove_all_jobs Delete all tasks from the specified task store , Or if no task is given , Delete all task stores .print_jobs Print out a text list of all tasks currently scheduled on all task repositories or only specific task repositories .The formal parameter return values involved in the above functions are not listed here , The main thing is to have an impression of the interface provided by each class , You can refer to the official documents when you really use it , Difficulty is not great
https://apscheduler.readthedocs.io/en/stable/py-modindex.html