APScheduler (scheduled task -Python Library)

APScheduler Profile

APScheduler (Advanced Python Scheduler) is a lightweight framework for task scheduling regular Python (Python library).
APScheduler has three built-in scheduling system, including:

  • cron scheduler (optional start / end time)
  • Based on the execution interval (interval of an even run the job, may be selected start / end time)
  • Disposable delay the execution of tasks (within the specified date / time to run the job once)

Supported back-end storage operations

And any mixture APScheduler backmatch job storage and dispatch system, wherein the rear support memory operations comprising:

  • Memory
  • SQLAlchemy
  • MongoDB
  • Redis
  • RethinkDB
  • ZooKeeper

Integrated Python framework

APScheduler within inherited several common framework for Python:

  • asyncio
  • peddled
  • tornado
  • qt

APScheduler download and install

Use pip installation:

pip install apscheduler
pip install apscheduler==3.6.3

If a timeout or there is other case, you can select:

# 法1使用豆瓣源下载
pip install -i https://pypi.doubanio.com/simple/ apscheduler
# 法2使用清华源下载
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apscheduler

If there is not, click on the link or pypi official website to download. Download and unzip into a directory with the same level setup.py file, open cmd, use the command to download:

python setup.py install

APScheduler components

APScheduler There are four components, namely:

  • Trigger (trigger), the trigger includes scheduling logic, each job has its own triggers to determine the next run time. In addition to their own initial configuration, the flip-flop is completely stateless.
  • Job memory (job store), stores the scheduled jobs, the job memory simply default to the job stored in memory, other memory operations sucked job saved in the database, when the job is stored in a persistent job memory when the data of the job will be serialized and deserialized when loaded, to be noted that the job scheduler can not share memory.
  • Actuator (executor), process running jobs, usually performed to a process or thread pool by submitting specified in the job callable, when the job is completed, the actuator will notify the scheduler.
  • A scheduler (scheduler), the memory configuration and the operation execution can be completed in the scheduler. E.g. add, modify, remove the job, according to different application scenarios, the scheduler may choose a different, optional section will next appear.

Introduction of the components

scheduler

  • BlockingScheduler: When the dispatcher is the only thing you use when you want to run.
  • BackgroundScheduler: When you do not run any other frameworks and want to use the scheduler in the background of your application execution (ie, using the charging post this way).
  • AsyncIOScheduler: When your program uses asyncio (an asynchronous frame) when using.
  • GeventScheduler: When your program uses gevent (high-performance concurrent Python framework) when using.
  • TornadoScheduler: When your program based Tornado (a web framework) when using.
  • TwistedScheduler: When your program uses Twisted (an asynchronous frame) when using
  • QtScheduler: time if your application is a Qt application can be used.

Job memory

If your application will be re-created every time you start a job, then use the default job memory (MemoryJobStore) can be, but in the case if you need to restart the application or scheduler Ben collapse of any course holds the job, you should Depending on your application environment to select a specific memory operations. For example: using Mongo or SQLAlchemy JobStore (support for most RDBMS)

Actuator

Choice of actuator depends on what you use the above framework, in most cases, use the default ThreadPoolExecutor has been able to meet the demand. If your application involves CPU-intensive operations, you may consider using ProcessPoolExecutor to use more CPU cores. You can also use both at the same time, the ProcessPoolExecutor as the second actuator.

trigger

When you schedule a job, you need to select a trigger for this job, when used to describe this operation is triggered, APScheduler There are three built-in flip-flop type:

  • date-time specified date
  • interval within a certain time interval how long execution time
  • cron and Linux crontab format compatibility, the most powerful

use

When you need to schedule the job, you need to select a trigger for the job, the job description to be triggered when, APScheduler 3 has a built-in types of triggers:

  • Create a scheduler (Scheduler)
  • Add a scheduled task (job store)
  • Run a scheduled task

Add Job

There are two ways you can add a new job:

  • add_job to add jobs
  • Decorator Add Job

Performed only once

import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
def job2(text):
    print('job2', datetime.datetime.now(), text)
scheduler = BlockingScheduler()
scheduler.add_job(job2, 'date', run_date=datetime.datetime(2019, 2, 25, 19, 5, 6), args=['text'], id='job2')
scheduler.start()

In the above example, only once on 2010-2-25 19:05:06, args passing a text parameter.

Intervals

The following simple example to a job executed once every five seconds:

import datetime
from apscheduler.schedulers.blocking import BlockingScheduler

def job1():
    print('job1', datetime.datetime.now())
scheduler = BlockingScheduler()
scheduler.add_job(job1, 'interval', seconds=5, id='job1')  # 每隔5秒执行一次
scheduler.start()

Day 1:30:50 performed once

from apscheduler.schedulers.blocking import BlockingScheduler  # 后台运行
sc = BlockingScheduler()
f = open('t1.text', 'a', encoding='utf8')
@sc.scheduled_job('cron', day_of_week='*', hour=1, minute='30', second='50')
def check_db():
    print(111111111111)
if __name__ == '__main__':
    try:
        sc.start()
        f.write('定时任务成功执行')
    except Exception as e:
        sc.shutdown()
        f.write('定时任务执行失败')
    finally:
        f.close()

Performed every few minutes:

import datetime
from apscheduler.schedulers.blocking import BlockingScheduler

def job1():
    print('job1', datetime.datetime.now())
scheduler = BlockingScheduler()
# 每隔2分钟执行一次, */1:每隔1分钟执行一次
scheduler.add_job(job1, 'cron', minute="*/2", id='job1') 
scheduler.start()

Executed once per hour:

import datetime
from apscheduler.schedulers.blocking import BlockingScheduler

def job1():
    print('job1', datetime.datetime.now())
scheduler = BlockingScheduler()
# 每小时执行一次
scheduler.add_job(job1, 'interval', hours=1, id='job1')
# 每小时执行一次,上下浮动120秒区间内
# scheduler.add_job(job1, 'interval', hours=1, id='job1', jitter=120)
scheduler.start()

Also See: APScheduler (Python of Cron) using a timed task summary | APScheduler | Python apscheduler performed once every two hours | Python apscheduler execution per minute

APScheduler Profile

APScheduler (Advanced Python Scheduler) is a lightweight framework for task scheduling regular Python (Python library).
APScheduler has three built-in scheduling system, including:

  • cron scheduler (optional start / end time)
  • Based on the execution interval (interval of an even run the job, may be selected start / end time)
  • Disposable delay the execution of tasks (within the specified date / time to run the job once)

Supported back-end storage operations

And any mixture APScheduler backmatch job storage and dispatch system, wherein the rear support memory operations comprising:

  • Memory
  • SQLAlchemy
  • MongoDB
  • Redis
  • RethinkDB
  • ZooKeeper

Integrated Python framework

APScheduler within inherited several common framework for Python:

  • asyncio
  • peddled
  • tornado
  • qt

APScheduler download and install

Use pip installation:

pip install apscheduler
pip install apscheduler==3.6.3

If a timeout or there is other case, you can select:

# 法1使用豆瓣源下载
pip install -i https://pypi.doubanio.com/simple/ apscheduler
# 法2使用清华源下载
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple apscheduler

If there is not, click on the link or pypi official website to download. Download and unzip into a directory with the same level setup.py file, open cmd, use the command to download:

python setup.py install

APScheduler components

APScheduler There are four components, namely:

  • Trigger (trigger), the trigger includes scheduling logic, each job has its own triggers to determine the next run time. In addition to their own initial configuration, the flip-flop is completely stateless.
  • Job memory (job store), stores the scheduled jobs, the job memory simply default to the job stored in memory, other memory operations sucked job saved in the database, when the job is stored in a persistent job memory when the data of the job will be serialized and deserialized when loaded, to be noted that the job scheduler can not share memory.
  • Actuator (executor), process running jobs, usually performed to a process or thread pool by submitting specified in the job callable, when the job is completed, the actuator will notify the scheduler.
  • A scheduler (scheduler), the memory configuration and the operation execution can be completed in the scheduler. E.g. add, modify, remove the job, according to different application scenarios, the scheduler may choose a different, optional section will next appear.

Introduction of the components

scheduler

  • BlockingScheduler: When the dispatcher is the only thing you use when you want to run.
  • BackgroundScheduler: When you do not run any other frameworks and want to use the scheduler in the background of your application execution (ie, using the charging post this way).
  • AsyncIOScheduler: When your program uses asyncio (an asynchronous frame) when using.
  • GeventScheduler: When your program uses gevent (high-performance concurrent Python framework) when using.
  • TornadoScheduler: When your program based Tornado (a web framework) when using.
  • TwistedScheduler: When your program uses Twisted (an asynchronous frame) when using
  • QtScheduler: time if your application is a Qt application can be used.

Job memory

If your application will be re-created every time you start a job, then use the default job memory (MemoryJobStore) can be, but in the case if you need to restart the application or scheduler Ben collapse of any course holds the job, you should Depending on your application environment to select a specific memory operations. For example: using Mongo or SQLAlchemy JobStore (support for most RDBMS)

Actuator

Choice of actuator depends on what you use the above framework, in most cases, use the default ThreadPoolExecutor has been able to meet the demand. If your application involves CPU-intensive operations, you may consider using ProcessPoolExecutor to use more CPU cores. You can also use both at the same time, the ProcessPoolExecutor as the second actuator.

trigger

When you schedule a job, you need to select a trigger for this job, when used to describe this operation is triggered, APScheduler There are three built-in flip-flop type:

  • date-time specified date
  • interval within a certain time interval how long execution time
  • cron and Linux crontab format compatibility, the most powerful

use

When you need to schedule the job, you need to select a trigger for the job, the job description to be triggered when, APScheduler 3 has a built-in types of triggers:

  • Create a scheduler (Scheduler)
  • Add a scheduled task (job store)
  • Run a scheduled task

Add Job

There are two ways you can add a new job:

  • add_job to add jobs
  • Decorator Add Job

Performed only once

import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
def job2(text):
    print('job2', datetime.datetime.now(), text)
scheduler = BlockingScheduler()
scheduler.add_job(job2, 'date', run_date=datetime.datetime(2019, 2, 25, 19, 5, 6), args=['text'], id='job2')
scheduler.start()

In the above example, only once on 2010-2-25 19:05:06, args passing a text parameter.

Intervals

The following simple example to a job executed once every five seconds:

import datetime
from apscheduler.schedulers.blocking import BlockingScheduler

def job1():
    print('job1', datetime.datetime.now())
scheduler = BlockingScheduler()
scheduler.add_job(job1, 'interval', seconds=5, id='job1')  # 每隔5秒执行一次
scheduler.start()

Day 1:30:50 performed once

from apscheduler.schedulers.blocking import BlockingScheduler  # 后台运行
sc = BlockingScheduler()
f = open('t1.text', 'a', encoding='utf8')
@sc.scheduled_job('cron', day_of_week='*', hour=1, minute='30', second='50')
def check_db():
    print(111111111111)
if __name__ == '__main__':
    try:
        sc.start()
        f.write('定时任务成功执行')
    except Exception as e:
        sc.shutdown()
        f.write('定时任务执行失败')
    finally:
        f.close()

Performed every few minutes:

import datetime
from apscheduler.schedulers.blocking import BlockingScheduler

def job1():
    print('job1', datetime.datetime.now())
scheduler = BlockingScheduler()
# 每隔2分钟执行一次, */1:每隔1分钟执行一次
scheduler.add_job(job1, 'cron', minute="*/2", id='job1') 
scheduler.start()

Executed once per hour:

import datetime
from apscheduler.schedulers.blocking import BlockingScheduler

def job1():
    print('job1', datetime.datetime.now())
scheduler = BlockingScheduler()
# 每小时执行一次
scheduler.add_job(job1, 'interval', hours=1, id='job1')
# 每小时执行一次,上下浮动120秒区间内
# scheduler.add_job(job1, 'interval', hours=1, id='job1', jitter=120)
scheduler.start()

Also See: APScheduler (Python of Cron) using a timed task summary | APScheduler | Python apscheduler performed once every two hours | Python apscheduler execution per minute

Guess you like

Origin www.cnblogs.com/qq834761298/p/12164717.html