Read the directory

4.1 Date Scheduling (the job is executed only once) 4.2 Interval: The job is executed at intervals 4.3 Cron: 1. Add task 2. Delete task 3. Suspend and continue task 4. 4.1, Date Scheduling (Jobs will be executed only once) 4.2, interval: 4.3 Cron: How to use the same crontab in Linux Modify task attributes 5. Obtain the Job list 6. Start & Close Task 3. Perform a mission every 5 seconds. 3. Perform tasks at 0, 10, 20, 30, 40, 50. 4. Until 2020-05-30, Schedule tasks once a week from Monday to Friday at 5:30 am. 5. Schedule tasks at 1,2,3 o ‘clock on the third Friday in June,7,8,11, and December. APScheduler is Advanced Python Scheduler, a lightweight Framework for scheduling Python tasks. It allows you to schedule periodic tasks like Cron, and supports Python functions or arbitrary callable objects.

The scheduler is one of the other components. You usually have only one scheduler in your application, and application developers usually don’t directly deal with job stores, schedulers, and triggers. Instead, the scheduler provides a proper interface to handle these. Configuring job stores and actuators can be done in the scheduler, such as adding, modifying, and removing jobs.

For different scenarios, you can choose the following scheduler:

BlockingScheduler: When the scheduler is the only thing running in your application. BackgroundScheduler: Used when you are not running any other framework and want the scheduler to run in the background of your application. AsyncIOScheduler: Used when your program uses Asyncio (an asynchronous framework). GeventScheduler: Used when your program uses GEvent (the high-performance Python concurrency framework). TornadoScheduler: Use it when your application is based on Tornado, a Web framework. TwistedScheduler: Use QtScheduler when your application uses Twisted (an asynchronous framework) : Use QtScheduler if your application is Qt.

BackgroundScheduler: The scheduler runs in a background thread without blocking the current thread.

from apscheduler.schedulers.background import BackgroundScheduler import time

scheduler = BackgroundScheduler()

Def job1(): print “%s: execute “% time.asctime()

scheduler.add_job(job1, ‘interval’, seconds=3) scheduler.start()

While True: pass job Store Job store is used to store scheduled jobs. The default job store is simply to store jobs in memory.

Jobstore provides interfaces for adding, deleting, modifying, and querying jobs in scheduler. Based on different storage modes, jobStore can be divided into the following types:

MemoryJobStore: Without serialization, jobs are stored in memory, and SQLAlchemyJobStore is operated in memory. All databases supported by SQLAlchemy can be used as Backend. Add, delete, alter, and query operations can be converted into SQL statements MongoDBJobStore of backend RedisJobStore: Backend RethinkDBJobStore: Backend ZooKeeperJobStore: ZooKeeper as backend 3. Executor An executor processes tasks and submits callable objects in scheduled tasks to a thread or process. The executor notifies the scheduler when the task is complete.

The most common executors are of two types:

When scheduling a task, you need to set up a triggers for it. The trigger determines the date/time at which the scheduled task will be executed and in what form.

APScheduler has three built-in triggers:

Date: specifies a certain point in time. The job is executed only once. Interval: Fixed intervals are executed periodically. Cron: Cron-style expressions are executed periodically and are used to run jobs periodically (within a specified period of time). Use the same crontab method as in Linux. Back to top 4.1, date The parameters for scheduling (the job will be executed only once) are as follows:

Run_date (datetime | STR) – job run date or time timezone (datetime. Tzinfo | STR) – specify the time zone

sched.add_job(job_function, ‘date’, run_date=date(2016, 12, 12), args=[‘text’])

sched.add_job(job_function, ‘date’, run_date=datetime(2016, 12, 12, 12, 0, 0), args=[‘text’])

4.2 interval: Every once in a while to perform a weekes = 0 | days = 0 | pump | = 0 minutes = 0 | seconds = 0, the start_date = None, end_date = None, timezone = None

Days (int) – Days (int) – Hours (int) – Hours (int) Minutes (int) – minutes (int) – seconds (int) – Start_date (datetime | STR) – start date end_date (datetime | STR) – end date timezone (datetime. Tzinfo | STR) – time zone

scheduler.add_job(my_job, ‘interval’, hours=2)

scheduler.add_job(my_job, ‘interval’, hours=2, start_date=’2017-9-8 21:30:00′, end_date=’2018-06-15 21:30:00)

@scheduler.scheduled_job(‘interval’, id=’my_job_id’, hours=2) def my_job(): print(“Hello World”) Use the same crontab mode as on Linux (year=None, month=None, day=None, week=None, day_of_week=None, hour=None, minute=None, second=None, start_date=None, end_date=None, timezone=None)

Except for week and day_of_week, their default values are *

For example, day=1, minute=20, This equals year= “, month= “, day=1, week= “, day_of_week= “, hour= “*”, minute=20, second=0 The work will be performed on the first day of each month for 20 minutes per hour

Expression parameter type description

  • All wildcards. Example: minutes=* that is, every minute

*/a All wildcards divisible by a. A-b all ranges A-B triggers A-B/C All ranges A-B and is divisible by C Triggers XTH y day of the week trigger. X for p1, y is week last x days A month, last week triggered a few last days Triggered a month on the last day of x, y, z all combination expression, expression can be combined to determine the value or above year (int | STR) – years, 4 digit month (int | STR) – months (range 1-12) day (int | STR) – days (range 1 to 31) week (int | STR) – weeks (range 1 to 53) day_of_week (int | STR) – Which day or week a few weeks (range 0 to 6 or mon, tue, wed and thu, fri, sat, sun) hour (int | STR) – (range 0-23) when minute (int | STR) – points (range 0-59) second (int | STR) – second (range 0-59) the start_date (datetime | STR) – the earliest start date (containing) end_date (datetime | STR) – end time (with) the latest timezone (datetime. Tzinfo | STR) – specify the time zone sched. Add_job (my_job, ‘cron, hour = 3, minute = 30) sched. Add_job (my_job,’ cron, day_of_week=’mon-fri’, hour=5, minute=30, end_date=’2017-10-30′)

@sched.scheduled_job(‘cron’, id=’my_job_id’, day=’last sun’) def some_decorated_task(): print(“I am printed at 00:00:00 on the last Sunday of every month!” )

How to use APSched? Installation PIP PIP install apscheduler source installation (pypi.python.org/pypi/APSche… Get started with Python Setup. py install

first.py

from apscheduler.schedulers.blocking import BlockingScheduler import time

Instantiate a scheduler

scheduler = BlockingScheduler()

Def job1(): print “%s: execute “% time.asctime()

Add a task and set the triggering mode to 3s

scheduler.add_job(job1, ‘interval’, seconds=3)

Start running the scheduler

Scheduler.start () executes output:

python first.py

Fri Sep 8 20:41:55 2017: Executing tasks Fri Sep 8 20:41:58 2017: Executing Tasks…

1. Add a task. Method 1: Call add_job()

Calling add_job() returns an instance of apscheduler.job. job, which can be used to change or remove the job

Job = scheduler.add_job(myfunc, ‘interval’, minutes=2)

This applies to jobs that do not change during application running

from apscheduler.schedulers.blocking import BlockingScheduler sched = BlockingScheduler()

A decorator

@sched.scheduled_job(‘interval’, id=’my_job_id’, seconds=5) def job_function(): print(“Hello World”)

start

Sched.start () returns to top 2

scheduler.add_job(myfunc, ‘interval’, minutes=2, id=’my_job_id’) scheduler.remove_job(‘my_job_id’)

job = scheduler.add_job(myfunc, ‘interval’, minutes=2) job.remove()

Back to top 3. Pause & Continue Tasks can be easily paused and resumed by the Job instance or by the scheduler itself. When a job is paused, the next run time is cleared until the job resumes and no more run time is calculated. To pause a job, use one of the following methods:

job = scheduler.add_job(myfunc, ‘interval’, minutes=2)

job.pause()

job.resume()

scheduler.add_job(myfunc, ‘interval’, minutes=2, Id =’my_job_id’) scheduler.pause_job(‘my_job_id’) scheduler.resume_job(‘my_job_id’

job.modify(max_instances=6, name=’Alternate name’)

Scheduler.reschedule_job (‘my_job_id’, trigger=’cron’, minute=’*/5′) Use print_jobs() to print a list of all formatted jobs. Use get_job(task ID) to get the job list for the specified task.

Apscheduler.get_jobs () returns to the top

The scheduler. The start () # open scheduler. Shotdown (wait = True | False) # close False regardless of whether the task execution, Forced to shut down three, some regular task 1 script, regular script runs daily early morning 00:30:30 task execution import datetime from apscheduler. Schedulers, blocking the import BlockingScheduler from app.untils.log_builder import sys_logging

Scheduler = BlockingScheduler() #

The scheduler is set to run once a day at 00:30:30 am

@scheduler.scheduled_job(“cron”, day_of_week=’*’, hour=’1′, minute=’30’, second=’30’) def rebate(): print “schedule execute” sys_logging.debug(“statistic scheduler execute success” + datetime.datetime.now().strftime(“%Y-%m-%d %H:%M:%S”))

if name == ‘main’: try: scheduler.start() sys_logging.debug(“statistic scheduler start success”) except (KeyboardInterrupt, SystemExit): Scheduler.shutdown () sys_logging.debug(“statistic Scheduler start-up fail”) 2. Tasks are executed every 5 seconds between 0 PM and 8 am every day.

Every five seconds between 0 PM and 8 am.

scheduler.add_job(my_job, ‘cron’,day_of_week=’‘,hour = ‘0-8’,second = ‘/5′)

3. Perform tasks at 0, 10, 20, 30, 40, 50.

Scheduler. Add_job (my_job, ‘cron’,day_of_week=”,minute =’ /10′) 4. The scheduled task is executed at 5:30 am from Monday to Friday until 2020-05-30

sched.add_job(my_job(),’cron’, day_of_week=’mon-fri’, hour=5, minute=30,end_date=’2020-05-30′)

sched.add_job(job_function, ‘cron’, day_of_week=’mon-fri’, hour=5, minute=30, end_date=’2016-12-31′)

5. Perform scheduled tasks at 1,2,3 on the third Friday of June,7,8,11, and December

Sched. add_job(job_function, ‘cron’, month=’6-8,11-12′, day=’3rd fri’, hour=’0-3′) 6. Run this program every 5 seconds

Sched. Add_job (my_job, ‘cron, second =’ * / 5 ‘) resources Python task scheduling module APScheduler:segmentfault.com/a/119000001… The official document: apscheduler. Readthedocs. IO/en/v3.3.0 / m… The API documentation: apscheduler. Readthedocs. IO/en/v3.3.0 / p… Python modules (APScheduler scheduled tasks) : blog.csdn.net/qq_37634812… Example of a scheduled task on Python: www.jb51.net/article/165… Python tags: Django uses Nginx + UWSgi + Django to build a Web site Nginx deployment Vue project record posted at @2020-01-11 00:34 Bread locker read (2946) comments (0) Edit favorite report