Apscheduler--python Cron

Source: Internet
Author: User

The Apscheduler is the Advanced Python Scheduler, a Lightweight Python task scheduling framework. It allows you to schedule recurring tasks like Cron, and supports Python functions or any callable object.

Installing Apscheduler

You can use the PIP tool to install:

Pip Install Apscheduler

or download the latest source package (https://pypi.python.org/pypi/APScheduler/) on PyPI to install:

Python setup.py Install

Simple Application (translated from official documents)

Start Scheduler with default configuration

 from Import Scheduler    = Scheduler () Sched.start ()

fromapscheduler.scheduler importScheduler

sched =Scheduler()

sched.start()

1. Fixed-time scheduling:

 fromDatetimeImportDate fromApscheduler.schedulerImportScheduler#Start SchedulerSched =Scheduler () Sched.start ( )#define functions that are automatically dispatcheddefmy_job (text):Printtext#define the execution time of a task (May 17, 2013)Exec_date = Date (2013, 5, 17)   #add to the task queue and assign it to variables to make it easier to cancel such operationsJob = Sched.add_date_job (My_job, Exec_date, ['text'])

2. Cycle tasks:

def job_function ():     Print " Hello World "    # Job_function will be executed once every two hours sched.add_interval_job (job_function, hours=2)    #  Same as the above tasks, but only after 2013-5-17 18:30 sched.add_interval_job (job_function, hours=2, start_date=' 2013-5-17 18:30 ')

Adorner version:

@sched. Interval_schedule (hours=2)def  job_function ():    print" Hello World "

Scheduling of 3.Cron-style tasks:

def job_function ():     Print " Hello World "   # The job_function function will be executed at 0, 1, 2 and 3 in the third Friday in June, July, November, and December, respectively , Sched.add_cron_job (job_function, month='  6-8,11-12', day='3rd Fri', hour='0-3  ')

Adorner version:

@sched. Cron_schedule (day=' lastSun')def  Some_decorated_task ():     Print " I am printed at 00:00:00 in the last Sunday of every month! "

More powerful methods of use can be further referenced in official documentation.

Apscheduler--python Cron

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.