Today is ready to implement a function need to use the timing of the task, so I saw a Python schedule task frame Apscheduler, try to feel good.
1.APScheduler Introduction:
Apscheduler is a timed task framework for Python that can easily meet the needs of the user for timed execution or periodic execution of tasks, providing a date-based, fixed-time interval interval, And a timed task similar to the crontab type of the scheduled task on Linux. And the framework can not only add, delete scheduled tasks, but also to store tasks in the database, to achieve the persistence of tasks, so it is very convenient to use.
2.APScheduler Installation:
Apscheduler installation is relatively simple, you can directly use the PIP installation, if no pip can download the source code, the use of source installation.
1). Install with PIP: (recommended)
# pip Install Apscheduler
2). Source-based installation: https://pypi.python.org/pypi/APScheduler/
# python setup.py Install
3. Basic Concepts
Apscheduler has four types of components and related instructions:
1) triggers (trigger): The trigger contains the dispatch logic, each job has its own trigger, which determines which job will run next, except that the trigger is completely stateless in addition to their own initialization configuration.
2) Job stores (Job store): used to store the scheduled job, the default job memory is simply to save the job task in memory, the other job storage can save the task to a variety of databases, support MongoDB, Redis, sqlalchemy storage mode. When the job task is persisted for storage, the data of the job is serialized and deserialized when the job is re-read.
3) Executors (actuator): The actuator is used to perform timed tasks, but only the tasks that need to be performed are placed in a new thread or thread pool. When the job task finishes, the executor notifies the scheduler. For actuators, Threadpoolexecutor is selected by default, but if you are involved in a particular task such as comparing CPU-consuming tasks, you can choose Processpoolexecutor, of course, depending on the actual needs of the two actuators can be used at the same time.
4) schedulers (Scheduler): The scheduler is the other parts of the association, generally in the application only one scheduler, application developers do not directly manipulate triggers, task stores and actuators, instead, the scheduler provides the interface for processing. Through the scheduler to complete the storage of tasks and the execution of the configuration operations, such as can be added. Modify, remove task jobs.
Apscheduler provides a variety of schedulers that can be selected according to the specific needs of the scheduler, the Common Scheduler is:
Blockingscheduler: For situations where a single task is run only in a process, it is usually used when the scheduler is the only thing you want to run.
Backgroundscheduler: It is appropriate to require any situation that runs in the background of the program when you want the scheduler to execute in the background of the application.
Asyncioscheduler: Suitable for use with Asyncio framework
Geventscheduler: Suitable for use with gevent framework
Tornadoscheduler: Suitable for applications using the Tornado framework
Twistedscheduler: Suitable for applications using the twisted framework
Qtscheduler: Suitable for use in QT situations
4. Configuring the Scheduler
Apscheduler provides a number of different ways to configure the scheduler, which you can pass in using a configuration dictionary or as a parameter keyword. You can also create a scheduler, configure and add jobs, so you get more flexibility in different environments.
1) Here is a simple example:
Import timefrom apscheduler.schedulers.blocking import Blockingscheduler def test_job (): Print Time.strftime ('%y-%m-%d %h:%m:%s ', Time.localtime (Time.time ())) Scheduler = Blockingscheduler () "#该示例代码生成了一个BlockingScheduler调度器, The default default task store, Memoryjobstore, and the default executor threadpoolexecutor are used, and the maximum number of threads is 10. "' Scheduler.add_job (test_job, ' interval ', seconds=5, id= ' test_job ') ' #该示例中的定时任务采用固定时间间隔 (interval) is performed every 5 seconds. #并且还为该任务设置了一个任务id ' Scheduler.start ()
2) If you want to perform some complex tasks, such as using both actuators at the same time as above, or using a variety of task storage methods, and you need to adjust some of the default parameters of the task according to specific circumstances. You can refer to the following method. (http://apscheduler.readthedocs.io/en/latest/userguide.html)
The first way:
from pytz import utcfrom Apscheduler.schedulers.background import backgroundschedulerfrom apscheduler.jobstores.mongodb import MongoDBJobStorefrom apscheduler.jobstores.sqlalchemy import sqlalchemyjobstorefrom apscheduler.executors.pool import threadpoolexecutor, processpoolexecutorjobstores = { ' MONGO ': mongodbjobstore (), ' default ': sqlalchemyjobstore (url= ' sqlite:///jobs.sqlite ')}executors = { ' default ': threadpoolexecutor (, ' Processpool ': Processpoolexecutor (5)}job_defaults = { ' COALESCE ': False, ' max_instances ': 3}scheduler = backgroundscheduler (jobstores=jobstores, EXECUTORS=EXECUTORS, JOB_DEFAULTS=JOB_DEFAULTS, TIMEZONE=UTC)
The second way:
from apscheduler.schedulers.background import backgroundscheduler# the "Apscheduler." prefix is hard codedscheduler = backgroundscheduler ({ ' Apscheduler.jobstores.mongo ': { ' type ': ' MongoDB ' }, ' Apscheduler.jobstores.default ': { ' type ': ' SQLAlchemy ', ' URL ': ' sqlite:///jobs.sqlite ' }, ' Apscheduler.executors.default ': { ' class ': ' Apscheduler.executors.pool:ThreadPoolExecutor ', ' max_workers ': ' }, ' Apscheduler.executors.processpool ': { ' type ': &nbsP; ' Processpool ', ' max_workers ': ' 5 ' }, ' Apscheduler.job_defaults.coalesce ': ' false ', ' Apscheduler.job_defaults.max_instances ': ' 3 ', ' apscheduler.timezone ': ' UTC ',} )
Third Way:
from pytz import utcfrom apscheduler.schedulers.background import backgroundschedulerfrom apscheduler.jobstores.sqlalchemy import sqlalchemyjobstorefrom apscheduler.executors.pool import processpoolexecutorjobstores = { ' MONGO ': {' type ': ' MongoDB '}, ' default ': sqlalchemyjobstore (url= ' SQLite :///jobs.sqlite ')}executors = { ' default ': {' type ': ' ThreadPool ', ' max_workers ': 20}, ' processpool ': processpoolexecutor (max_workers=5)} job_defaults = { ' coalesce ': false, ' max_ Instances ': 3}scheduler = backgroundscheduler () # do something else here, maybe add jobs etc.scheduler.configure ( jobstores=jobstores, executors=executors, job_defaults=job_defaults, TIMEZONE=UTC)
5. Basic operations for Task jobs:
1). There are two ways to add a job: The first is to call Add_job () directly, and the second is to use the Scheduled_job () modifier.
While Add_job () is the most used, it can return a Apscheduler.job.Job instance, so it can be modified or deleted, and the task added with the decorator cannot be modified after it has been added.
For example, use Add_job () to add a job:
#!/usr/bin/env python#-*- coding:utf-8import timeimport datetimefrom Apscheduler.schedulers.blocking import blockingschedulerdef job1 (f): Print time.strftime ('%y-%m-%d %h:%m:%s ', time.localtime (Time.time ())), fdef job2 (arg1 , args2, f): print f, args1, args2def job3 (**args): print args ' Apscheduler supports the following three scheduled tasks: Cron: crontab type task interval: fixed interval task Date: One-time task based on DateTime ' Scheduler = blockingscheduler () #循环任务示例scheduler. Add_job (job1, ' interval ', seconds=5, args= (' cycle ',), id= ' Test_job1 ') #定时任务示例scheduler. Add_job (job1, ' cron ', Second= ' */5 ', args= (' timed ',), id= ' Test_job2 ') #一次性任务示例scheduler. Add_job (Job1, next_run_time= ( Datetime.datetime.now () + datetime.timedelta (seconds=10), args= (' Once ',), id= ' test_job3 ') ' ' The way parameters are passed in a tuple (tuple), list, dictionary (dict) Note: However, you need toNote that you need to add a comma "' #基于listscheduler after passing the parameter in the tuple. Add_job (job2, ' interval ', seconds=5, args=[' a ', ' B ', ' List '], id= ' test_job4 ') #基于tuplescheduler. Add_job (job2, ' interval ', seconds=5, args= (' A ', ' B ', ' tuple ',), id= ' test_job5 ') #基于dictscheduler. Add_job (job3, ' interval ', seconds=5, kwargs={ ' F ': ' Dict ', ' a ': 1, ' B ': 2}, id= ' test_job7 ') print scheduler.get_jobs () Scheduler.start () # Example Scheduler.add_job with parameters (job2, ' interval ', seconds=5, args=[' a ', ' B '], id= ' TEST_JOB4 ') Scheduler.add_job (job2, ' interval ', seconds=5, args= (' A ', ' B ',), id= ' test_job5 ') Scheduler.add_job (job3, ' interval ', seconds=5, kwargs={' a ': 1, ' B ': 2}, id= ' test_job6 ') print scheduler.get_jobs () Scheduler.start ()
or use the Scheduled_job () decorator to add a job:
@sched. Scheduled_job (' cron ', second= ' */5 ', id= ' my_job_id ',) def test_task (): Print ("Hello world!")
2). Get a list of tasks:
You can use the Get_jobs method to get the current task list, or you can use Get_job () to get information about a task based on job_id. And Apscheduler also provides a print_jobs () method to print a formatted list of tasks.
For example:
Scheduler.add_job (my_job, ' interval ', seconds=5, id= ' my_job_id ' name= ' test_job ') print scheduler.get_job (' my_job_id ') ) Print scheduler.get_jobs ()
3). Modify the Task:
Modifying the properties of a task task can use the apscheduler.job.Job.modify () or Modify_job () method to modify any other property except the ID.
For example:
Job = Scheduler.add_job (my_job, ' interval ', seconds=5, id= ' my_job ' name= ' test_job ') job.modify (max_instances=5, Name= ') My_job ')
4). Delete Task:
Deleting a task in the scheduler can use Remove_job () to delete a specified task based on the job ID or remove (), and if you use remove () you need to save the instance object that was returned when you added the task, and the task is not executed after it is deleted.
Note: Tasks added through Scheduled_job () can only be deleted using Remove_job ().
For example:
Job = Scheduler.add_job (my_job, ' interval ', seconds=5, id= ' my_job_id ' name= ' test_job ') job.remove ()
Or
Scheduler.add_job (my_job, ' interval ', seconds=5, id= ' my_job_id ' name= ' test_job ') scheduler.remove_job (' My_job ')
5). Pause and Resume tasks:
Pause and resume tasks can be implemented directly by manipulating task instances or schedulers. When a task is paused, its run time is reset and time is not calculated during the pause.
To pause a task:
Apscheduler.job.Job.pause () Apscheduler.schedulers.base.BaseScheduler.pause_job ()
Restore tasks
Apscheduler.job.Job.resume () Apscheduler.schedulers.BaseScheduler.resume_job ()
6). Start the Scheduler
The scheduler can be started using the start () method, Blockingscheduler needs to be initialized before start () is executed, and for other scheduler, the call to start () method will return directly, and then the subsequent initialization operation can proceed.
For example:
From apscheduler.schedulers.blocking import Blockingscheduler def my_job (): print "Hello world!" scheduler = BLOCKINGSC Heduler () scheduler.add_job (my_job, ' interval ', seconds=5) Scheduler.start ()
7). Close the Scheduler:
Use the following method to close the scheduler:
Scheduler.shutdown ()
By default, the scheduler shuts down its task store and executor and waits for all the tasks that are being performed to complete, and if you do not want to wait, you can do the following:
Scheduler.shutdown (Wait=false)
Attention:
When no handlers could is found for logger "Apscheduler.scheduler" error message, the description does not
The logger of the logging module is present, so it needs to be added, the corresponding additions are as follows (for reference only):
Import Logging Logging.basicconfig (level=logging. DEBUG, format= '% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ', datafmt= '%a,%d %b%Y%h:%m:%s ', filename= '/var/log/aaa.txt ', filemode= ' a ')
Simple use of apscheduler under Python