Python APScheduler timed task framework

Posted by SwiftlyTilting on Wed, 26 Jan 2022 19:28:26 +0100

Python APScheduler timed task framework

0. Introduction

APScheduler is based on a python timing task framework of Quartz, which realizes all functions of Quartz and is very convenient to use. It provides tasks based on date, fixed time interval and crontab type, and can persist tasks. Based on these functions, we can easily implement a python timed task system.

  • github: https://github.com/agronholm/apscheduler

  • Official website documents: https://apscheduler.readthedocs.io/en/latest/

1. Installation

pip install apscheduler

2. Composition

The whole APScheduler system can be said to be composed of these five concepts:

  • Triggers contain scheduling logic. Each job has its own trigger to decide which job will run next. In addition to their own initial configuration, triggers are completely stateless.
  • The job store stores the scheduled jobs. The default job store simply saves the jobs in memory. Other job stores store the jobs in the database. The data of a job is serialized when saved in the persistent job store and deserialized when loaded. The scheduler cannot share the same job store.
  • The executor handles the operation of the job. They usually submit the specified callable object to a thread or enter the city pool in the job. When the job is completed, the executor will notify the scheduler.
  • The scheduler is an integral part of other. You usually have only one scheduler in your application. The application developer usually does not directly deal with job storage, scheduler and trigger. On the contrary, the scheduler provides an appropriate interface to deal with these. Configuring job storage and executors can be done in the scheduler, such as adding, modifying, and removing jobs.
  • job: describes a task itself.

3. Scheduler IO model

Due to different IO models, scheduler can be implemented in many ways:

  • BlockingScheduler: main_loop runs in the main thread of the current process, so calling the start function will block the current thread. Through a threading The event condition variable object completes the scheduled wake-up of the scheduler.
  • BackgroundScheduler: basically the same as BlockingScheduler, except main_loop is placed in a separate thread, so the main thread will not block after calling start
  • Asyncio scheduler: asyncio is used as the scheduler of IO model. It is used in conjunction with asyncio executor. Event in asyncio is used_ Call of loop_ Later completes timed wake-up
  • GeventScheduler: basically the same as BlockingScheduler, gevent is used as the IO model and used in conjunction with GeventExecutor
  • QtScheduler: use QTimer to complete timed wake-up
  • Tornado scheduler: uses tornado's IO model and ioloop add_ Timeout completes the timed wake-up
  • TwistedScheduler: cooperate with twistexecutor and use reactor Calllater completes timed wake-up

4. Examples

a.BlockingScheduler

from apscheduler.schedulers.blocking import BlockingScheduler
def my_job():
    print 'hello world'
  
scheduler = BlockingScheduler()
scheduler.add_job(my_job, 'interval', seconds=5)
scheduler.start()

The above example shows that my is executed every 5s_ Job function, output hello world

b.BackgroundScheduler

from apscheduler.schedulers.background import BackgroundScheduler
 
def job3():
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
    print("I'm working job_3")
 
scheduler = BackgroundScheduler()
scheduler.add_job(job3, 'interval', seconds=3)  #Execute every 3 seconds
scheduler.start()

5. Trigger control

a.interval scheduling

How often

weeks (int) – number of weeks to wait
days (int) – number of days to wait
hours (int) – number of hours to wait
minutes (int) – number of minutes to wait
seconds (int) – number of seconds to wait
start_date (datetime|str) – starting point for the interval calculation
end_date (datetime|str) – latest possible date/time to trigger on
timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations
#It means that the task is executed every 3 days at 17:19:07
sched.add_job(my_job, 'interval', days=03, hours=17, minutes=19, seconds=07)
#Execute every 3 seconds
scheduler.add_job(job3, 'interval', seconds=3)

b.date scheduled scheduling

The job is executed only once

run_date (datetime|str) – the date/time to run the job at  -(Task start time)
timezone (datetime.tzinfo|str) – time zone for run_date if it doesn't have one already
#Execute only once at the specified time
scheduler.add_job(tick, 'date', run_date='2016-02-14 15:01:05')  
# The job will be executed on November 6th, 2009
sched.add_job(my_job, 'date', run_date=date(2009, 11, 6), args=['text'])
# The job will be executed on November 6th, 2009 at 16:30:05
sched.add_job(my_job, 'date', run_date=datetime(2009, 11, 6, 16, 30, 5), args=['text'])

c.cron timing scheduling

(int|str) Indicates that the parameter can be either int Type, or str type
(datetime | str) Indicates that the parameter can be either datetime Type, or str type
 
year (int|str) – 4-digit year -(Indicates a four digit year, such as 2008)
month (int|str) – month (1-12) -(Indicates that the value range is 1-12 (month)
day (int|str) – day of the (1-31) -(Indicates that the value range is 1-31 (day)
week (int|str) – ISO week (1-53) -(December 31, 2006 in the Gregorian calendar can be written as 2006-W52-7(Extended form) or 2006 W527(Compact form)
day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun) - (Represents the day of the week. You can use 0-6 (it can also be expressed by its English abbreviation)
hour (int|str) – hour (0-23) - (Indicates that the value range is 0-23 Time)
minute (int|str) – minute (0-59) - (Indicates that the value range is 0-59 (min)
second (int|str) – second (0-59) - (Indicates that the value range is 0-59 (seconds)
start_date (datetime|str) – earliest possible date/time to trigger on (inclusive) - ((indicates start time)
end_date (datetime|str) – latest possible date/time to trigger on (inclusive) - ((indicates end time)
timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone) -(Indicates time zone (value)

Parameter value format:

#It means that the procedure was executed at 17:19:07 on March 22, 2017
sched.add_job(my_job, 'cron', year=2017,month = 03,day = 22,hour = 17,minute = 19,second = 07)
  
#It means that the task executes the procedure at 00:00, 01:00, 02:00 and 03:00 on the third Friday in June, July, August, November and December
sched.add_job(my_job, 'cron', month='6-8,11-12', day='3rd fri', hour='0-3')
  
#It means from 5:30 (AM) from Monday to Friday until 00:00:00 on May 30, 2014
sched.add_job(my_job(), 'cron', day_of_week='mon-fri', hour=5, minute=30,end_date='2014-05-30')
  
#Indicates that the program is executed every 5 seconds, which is equivalent to seconds = 5 in interval scheduling
sched.add_job(my_job, 'cron',second = '*/5')

6. Reference articles

https://www.cnblogs.com/shhnwangjian/p/7877985.html

Topics: Python Back-end