程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python apscheduler API summary

編輯:Python

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

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

event Corresponding enumeration value describe Belonging class EVENT_SCHEDULER_STARTED1 Scheduler start SchedulerEventEVENT_SCHEDULER_SHUTDOWN2 The scheduler closes SchedulerEventEVENT_SCHEDULER_PAUSED4 Task processing pauses in the scheduler SchedulerEventEVENT_SCHEDULER_RESUMED8 Task processing recovery in the scheduler SchedulerEventEVENT_EXECUTOR_ADDED16 Add the actuator to the scheduler SchedulerEventEVENT_EXECUTOR_REMOVED32 The actuator removes from the scheduler SchedulerEventEVENT_JOBSTORE_ADDED64 Add the task store to the scheduler SchedulerEventEVENT_JOBSTORE_REMOVED128 The task store is deleted from the scheduler SchedulerEventEVENT_ALL_JOBS_REMOVED256 All tasks are deleted from all task stores or from a specific task store SchedulerEventEVENT_JOB_ADDED512 Add task to task store JobEventEVENT_JOB_REMOVED1024 The task... Was deleted from the task store JobEventEVENT_JOB_MODIFIED2048 The task was modified from outside the scheduler JobEventEVENT_JOB_EXECUTED4096 The task was successfully executed JobExecutionEventEVENT_JOB_ERROR8192 The task threw an exception during execution JobExecutionEventEVENT_JOB_MISSED16384 Missed task execution JobExecutionEventEVENT_JOB_SUBMITTED32768 The task has been submitted to the actuator for execution JobSubmissionEventEVENT_JOB_MAX_INSTANCES65536 When the task reaches the maximum concurrent execution , Triggered events JobSubmissionEventEVENT_ALL Include all of the above events

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 :

SchedulerEvent

  • alias Add or remove the alias of the task storage or execution program ( If there is )

JobEvent

  • job_id Identification of the task ID
  • jobstore Alias of the task store that contains the related task

JobSubmissionEvent

  • job_id Identification of the task ID
  • jobstore Alias of the task store that contains the related task
  • scheduled_run_times List of times when the task is scheduled to run

JobExecutionEvent

  • job_id Identification of the task ID
  • jobstore 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

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

Job(scheduler, id=None, **kwargs)

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

modify(**changes)

  • 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

pasue()

  • Return type Job Example

Pause the execution of the task

remove()

Cancel the task , And delete it from its associated task store

reschedule(trigger, **trigger_args)

  • Return type Job Example

Switch triggers on this task

resume()

  • Return type Job Example

If previously suspended , Resume the task's schedule

property pending

If the referenced task is still waiting to be added to its specified task store , Then return to True.

triggers

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

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

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

cron

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 :

expression describe * All worth */a from a Values start , Trigger each a value a-b Yes a-b Any value in the range (a Must be less than b)a-b/c stay a-b Trigger each in the range c value xth y On the first day of the month x individual Zhou y Trigger ( x Supported parameters 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

combining

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)

BaseJobStore

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

add_job(job)

  • job(Job) Tasks to be added
  • abnormal ConflictingIdError Identifier of the task job_id Throw an exception when there is the same

Add a task to the task store

get_all_jobs()

  • 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

get_due_jobs(now)

  • 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

get_next_run_time()

  • Return type datetime.datetime

Returns the earliest running time of all tasks stored in this task store or None( If there are no active tasks )

lookup_job(job_id)

  • job_id(str|unicode) Identifier of the task
  • Return type Job

Return to a specific task , perhaps None ( Can't find )

remove_all_jobs()

Delete all tasks from the task store

remove_job(job_id)

  • job_id(str|unicode) Identifier of the task
  • abnormal JobLookupError If the task does not exist

Delete the specified task from the task store

shutdown()

Release all resources on the task store

start(scheduler,alias)

  • scheduler The scheduler that manages this task store
  • alias Alias for this task store

update_job(job)

  • job Tasks to update
  • abnormal JobLookupError If the task does not exist

Replace the task in the task store with the given newer version

BaseExecutor

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

shutdown(wait=True)

  • wait(bool) Set to True Then wait until all the tasks being executed in the actuator end and stop

Stop this actuator

start(scheduler, alias)

  • 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

submit_job(job, run_times)

  • job(Job) The work to be performed
  • run_times(list[datetime]) A datetime list that specifies when the task should run
  • abnormal MaxInstancesReachedError The exception thrown when the maximum number of instances that this task can run simultaneously is reached

Submit a task to perform

BaseScheduler

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

BaseScheduler(gconfig={}, **options)

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

Control scheduler

Method describe abnormal configure Reset the configuration for a given scheduler 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 .

Control actuator

Method describe abnormal add_executor Add an executor to this scheduler .ValueError – If there is already an executable with the given alias remove_executor Remove the executor with the given alias from this scheduler .

Control task storage

Method describe abnormal add_jobstore Add a task store to 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 .

Control event listeners

Method describe add_listener Add a listener for scheduler Events .remove_listener Delete the previously added event listener .

Control the mission

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

Method describe abnormal add_job Add the given task to the task list , If the scheduler is already running , Then wake it up .scheduled_job Use 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 Jobremove_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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved