The Apscheduler module in Python

Source: Internet
Author: User

1. Introduction

Apscheduler is a task Timing module in Python that contains four components: Trigger (trigger), Job store (Job store), actuator (executor), Scheduler (Scheduler).

2. Installation

Pip Install Apscheduler

3. Example

1 #Coding=utf-82  fromApscheduler.schedulers.blockingImportBlockingscheduler3 4 #Job 15 defmy_job1 ():6     Print 'Hello world!'7 8 #Job 29 defmy_job2 (name):Ten     Print 'Hello World,', name One  A #run the function once every five seconds -Sched =Blockingscheduler () - #functions without parameters and with parameters theSched.add_job (MY_JOB1,'interval', seconds=5) -Sched.add_job (FUNC=MY_JOB2, args= ('Tom',), trigger='interval', seconds=5) -Sched.start ()

4. Explanation

About triggers (trigger), it has three parameters to choose from: Date/interval/cron.

Date: A one-time task that executes only once.

The parameters are as follows:

Next_run_time (DATETIME|STR), Haven Date/time to run, the job at

TimeZone (DATETIME.TZINFO|STR) –time zone for Run_date if it doesn ' t has one already

Examples are as follows:

# time delay five seconds after Sched.add_job (Func=my_job2, args= ('Tom'), trigger='  Date'next_run_time=now+datetime.timedelta (seconds=5))

Interval: A recurring task, that is, performing tasks by time interval.

The parameters are as follows:

weeks (int) –number of weeks to wait
Days (int) –number of days to wait
hours (int) –number of hours to wait
minutes (int) –number of minutes to wait
seconds (int) –number of seconds to wait
start_date (DATETIME|STR) –starting point for the interval calculation
end_date (DATETIME|STR) –latest possible date/time to trigger on
TimeZone (DATETIME.TZINFO|STR) –time zone to use for the Date/time calculations

Examples are as follows:

# perform tasks once every five seconds sched.add_job (Func=my_job2, args= ('Tom',), trigger='  Interval', seconds=5)

Cron: A timed task that executes a task at each time period.

The parameters are as follows:

Year (INT|STR) –4-digit year
Month (INT|STR) –month (1-12)
Day (INT|STR) –day of the (1-31)
Week (INT|STR) – Week (1-53)
Day_of_week (INT|STR) –number or name of weekday (0-6 or Mon,tue,wed,thu,fri,sat,sun)
Hour (INT|STR) –hour (0-23)
minute (INT|STR) –minute (0-59)
Second (INT|STR) –second (0-59)
start_date (DATETIME|STR) –earliest possible date/time to trigger on (inclusive)
end_date (DATETIME|STR) –latest possible date/time to trigger on (inclusive)
TimeZone (DATETIME.TZINFO|STR) –time zone to use for the Date/time calculations (defaults to scheduler timezone)

Examples are as follows:

# in 1-3, August-October, every day 5 o'clock in the afternoon, every minute performs a task sched.add_job (func=my_job1, trigger='cron', Month='1-3,8-10', day='*', hour='17  ', minute='*')

The Apscheduler module in Python

Related Article

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.