Python third-party library series 13-timed Task Apscheduler Library

Source: Internet
Author: User
Tags throw exception
Apscheduler is a Python timing task framework based on Quartz, which realizes all the functions of quartz and is very convenient to use. Provides tasks based on dates, fixed intervals, and crontab types, and can persist tasks. Based on these features, we can easily implement a Python timing task system, writing Python is much more comfortable than Java.

1. Examples of timed tasks

Apscheduler is a scheduler within the process that can trigger specific functions at timed intervals and can access all the variables and functions applied. It is convenient to implement timed tasks through Apscheduler in Web applications. Here's a look at the example:

From Apscheduler.scheduler Import Scheduler  

Schedudler = Scheduler (Daemonic = False)  

@schedudler. Cron_ Schedule (second= ' * ', day_of_week= ' 0-4 ', hour= ' 9-12,13-15 ')  
def quote_send_sh_job ():  
    print ' A simple cron job Start at ', Datetime.datetime.now ()  
  
Schedudler.start ()  

A cron job is defined above through the adorner, which can be added through function scheduler.add_cron_job, and is more convenient with adorners.  The scheduler constructor passes in the Daemonic parameter, indicating that the thread of execution is not guarded and that the non-daemon thread is recommended in Schduler documentation: (Jobs are always executed in non-daemonic threads. )

Three ways to timed tasks:

(1) Simple date-based scheduling (timed task, fixed time, execute once)

From datetime import date from
Apscheduler.scheduler Import Scheduler

# Start the scheduler
sched = Scheduler ()
Sched.start ()

# Example: Demand: In 2013-1-4 13:14:21 print I love you</span>
def my_job (text):
   print Text

# Store The job in a variable into case we want to cancel the
first parameter of the It # method is the name of the method that needs to be executed, the second parameter is the time, and the third parameter is the parameter list of the method that needs to be executed 
  job = Sched.add_date_job (my_job, ' 2013-01-04 13:14:21 ', [' I Love You ']) </span>

(2) interval-based scheduling (performed once every how long)

# Example: requirements: Print every one hours Hello world</span>
job = sched.add_interval_job (my_job,hour=1,[' Hellow World ')

(3) Cron-style scheduling (regular cycle execution, such as the number of each month, or the number of weeks per week, or the first week of the year to perform)

# when not set minutes and seconds defaults to 0,example: requirements: Every Monday, three, five printing Hello World (Sunday is 0, Saturday is 6)
job = sched.add_cron_job (my_job,day-of-week= ' 0,2,4 ', [ ' Hellow World ']

More parameters move to the official website: https://apscheduler.readthedocs.org/en/v2.1.0/modules/scheduler.html

There is also a more important parameter max_instances when you add a job, specifying the number of concurrent instances of a job with a default value of 1. By default, if a job is ready to execute, but the previous instance of the job has not finished executing, the latter job fails and can be changed by this parameter.

2. Storing task execution information

Apscheduler provides jobstore for storing job execution information, by default Ramjobstore, and Sqlalchemyjobstore, Shelvejobstore, and Mongodbjobstore. Apscheduler allows multiple jobstore to be used at the same time, by Alias (alias), when adding a job needs to specify a specific Jobstore alias, otherwise the alias is the jobstore of default, that is, Ramjobstore. The following examples are illustrated with Mongodbjobstore.

Import Pymongo from  
apscheduler.scheduler Import Scheduler from  
Apscheduler.jobstores.mongodb_store Import Mongodbjobstore  
Import time  
  
sched = Scheduler (Daemonic = False)  
  
MONGO = Pymongo. Connection (host= ' 127.0.0.1 ', port=27017)  
store = Mongodbjobstore (Connection=mongo)  
Sched.add_jobstore ( Store, ' MONGO ')        # alias is Mongo  
 
@sched. Cron_schedule (second= ' * ', day_of_week= ' 0-4 ', hour= ' 9-12,13-15 '), jobstore= ' MONGO ']        # Add Job def job to Jobstore alias Mongo (  
):  
    print ' A job '  
    time.sleep (1)  
  
Sched.start ()  

Note that start must be called after the job action is added, otherwise it will be thrown incorrectly. The job information is saved by default to the Jobs table under the Apscheduler database:

> Db.jobs.findOne ()  
{  
        "_id": ObjectId ("502202d1443c1557fa8b8d66"),  
        "runs": "  
        Name": "Job",  
        "Misfire_grace_time": 1,  
        "coalesce": True,  
        "args": Bindata (0, "Gajdcqeu"),  
        "Next_run_time": Isodate ("2012-08-08t14:10:46z"),  
        "max_instances": 1,  
        "max_runs": null,  
        "trigger": Bindata (0, "xxx ...") ),  
        "Func_ref": "__main__:job",  
        "Kwargs": Bindata (0, "Gaj9cqeu")  
}  

The above is the specific information stored.
3. Exception Handling

When the job throws an exception, Apscheduler will swallow him silently, without giving any hint, this is not a good practice, we must know any error of the procedure. Apscheduler provides the registration listener, can listen to some events, including: Job throw exception, job not time to execute, etc.

Look at the example below, listening for exceptions and Miss events, where the logging module prints the log, and logger.exception () can print out the exception stack information.

def err_listener (EV):  
    Err_logger = Logging.getlogger (' schederrjob ')  
    if ev.exception:  
        err_ Logger.exception ('%s error. ', str (ev.job))  
    else:  
        err_logger.info ('%s Miss ', str (ev.job)  
  
) Schedudler.add_listener (Err_listener, Apscheduler.events.EVENT_JOB_ERROR | apscheduler.events.EVENT_JOB_MISSED)  

The properties of the event include:

Job–the job instance in question Scheduled_run_time–the time the job is scheduled to is
run
retval–the Return value of the successfully executed job
Exception–the exception raised by the job
traceback–the Ack object associated with the exception

Finally, note that when the job does not run in daemon mode and Apscheduler is not daemon, Ctrl + C is not effective when the script is closed, and must be kill. You can implement a shutdown script by using a command:

PS Axu | grep {script Name} | Grep-v grep | awk ' {print $} ' | Xargs Kill  



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.