Python scheduling framework Apscheduler use of detailed

Source: Internet
Author: User

# coding=utf-8 2 "" 3 demonstrates how to use the background scheduler to schedule a job that executes on 3 second 4 inte Rvals. 5 "" "6 7 from datetime import datetime 8 Import Time 9 import os10 one from Apscheduler.schedulers.background import back GroundScheduler12 def tick (): Print (' tick! The time is:%s '% DateTime.Now ()) + if __name__ = = ' __main__ ': Scheduler = Backgroundscheduler () schedu Ler.add_job (tick, ' interval ', seconds=3) #间隔3秒钟执行一次21 scheduler.start () #这里的调度任务是独立的一个线程22 print (' Press ctrl+{ 0} to exit '. Format (' break ' if os.name = = ' nt ' Else ' C ') * try:25 # This was here to simulate application AC             Tivity (which keeps the main thread alive). While True:27 Time.sleep (2) #其他任务是独立的线程执行28 Print (' sleep! ') Except (Keyboardinterrupt, systemexit): No strictly necessary if Daemonic mode is enabled but should b E done if Possible31 Scheduler.shutdown () print (' Exit the job! ') 

Non-blocking scheduling, executed once at a specified time

 1 # coding=utf-8 2 "" "3 demonstrates how to use the background scheduler to schedule a job that executes on 3 second 4 I Ntervals. 5 "" "6 7 from datetime import datetime 8 Import Time 9 import os10 one from Apscheduler.schedulers.background import back GroundScheduler12 def tick (): Print (' tick! The time is:%s '% DateTime.Now ()) If __name__ = = ' __main__ ': Scheduler = Backgroundscheduler () #sched Uler.add_job (tick, ' interval ', seconds=3) scheduler.add_job (tick, ' Date ', run_date= ' 2016-02-14 15:01:05 ') #在指定的时间, only Executes a scheduler.start () #这里的调度任务是独立的一个线程23 print (' Press ctrl+{0} to exit '. Format (' break ' if os.name = = ' NT ' el         Se ' C ')): Try:26 # This is a here-to-simulate application activity (which keeps the main thread alive). 27 While True:28 Time.sleep (2) #其他任务是独立的线程执行29 print (' sleep! ') Except (Keyboardinterrupt, systemexit): # Not strictly necessary if Daemonic mode is enabled BUT should be-done if Possible32 Scheduler.shutdown () print (' Exit the job! ') 

Non-blocking way, using cron execution

 1 # coding=utf-8 2 "" "3 demonstrates how to use the background scheduler to schedule a job that executes on 3 second 4 I Ntervals. 5 "" "6 7 from datetime import datetime 8 Import Time 9 import os10 one from Apscheduler.schedulers.background import back GroundScheduler12 def tick (): Print (' tick! The time is:%s '% DateTime.Now ()) If __name__ = = ' __main__ ': Scheduler = Backgroundscheduler () #sched Uler.add_job (tick, ' interval ', seconds=3) #scheduler. Add_job (tick, ' Date ', run_date= ' 2016-02-14 15:01:05 ') Sch Eduler.add_job (tick, ' cron ', day_of_week= ' 6 ', second= ' */5 ') "(int|str) –4-digit year25 mo         Nth (INT|STR) –month (1-12) 1-53 Day (INT|STR) –day of the (1-31) week (INT|STR)-ISO week () 28  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) 34  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) * All fire on E Very value38 */a any fire every a values, starting from the Minimum39 A-B any fire in any Valu         e within the-B range (a must be smaller than B) + a-b/c any fire every C values within the A-D range41 Xth y day fire in the x-th occurrence of weekday y within the MONTH42 last X day fire on the Las    T occurrence of weekday x within the MONTH43 last day fire in the last day within the month44 x, Y, Z any matching expression; Can combine any number of any of the above Expressions45 "Scheduler.start () #这里的调度任务是独立A thread of print (' press ctrl+{0} to exit '. Format (' break ' if os.name = = ' nt ' Else ' C ')) try:50 Here to simulate application activity (which keeps the main thread alive). Wuyi while true:52 Time.sleep ( 2) #其他任务是独立的线程执行53 print (' sleep! ') Except (Keyboardinterrupt, systemexit): # Not strictly necessary if Daemonic mode was enabled but should b E done if possible56 Scheduler.shutdown () print (' Exit the job! ')

Blocked way, 3 seconds interval execution

1 # coding=utf-8 2 "" "3 demonstrates how to use the background scheduler to schedule a job that executes on 3 second 4 in Tervals. 5 "" "6  7 from datetime import datetime 8 import OS 9 from apscheduler.schedulers.blocking import Blockingscheduler def tick ():     print (' tick! The time is:%s '% DateTime.Now ()) If __name__ = = ' __main__ ':     scheduler = Blockingscheduler ()     Schedule R.add_job (tick, ' interval ', seconds=3)     print (' Press ctrl+{0} to exit '. Format (' break ' if os.name = = ' NT ') Else ' C '))     try:24         scheduler.start ()    #采用的是阻塞的方式, there is only one thread dedicated to the task of scheduling     except (Keyboardinterrupt, Systemexit):         strictly necessary if Daemonic mode is enabled but should being done if Possible27         scheduler.s Hutdown ()         print (' Exit the job! ')

Use a blocking method to perform only once

1 # coding=utf-8 2 "" "3 demonstrates how to use the background scheduler to schedule a job that executes on 3 second 4 in Tervals. 5 "" "6  7 from datetime import datetime 8 import OS 9 from apscheduler.schedulers.blocking import Blockingscheduler def tick ():     print (' tick! The time is:%s '% DateTime.Now ()) If __name__ = = ' __main__ ':     scheduler = Blockingscheduler ()     Schedule R.add_job (tick, ' Date ', run_date= ' 2016-02-14 15:23:05 ')     print (' Press ctrl+{0} to exit '. Format (' Break ' if Os.name = = ' nt ' Else ' C ')     try:24         scheduler.start ()    #采用的是阻塞的方式, there is only one thread dedicated to scheduling the task of     except ( Keyboardinterrupt, Systemexit): $         # not strictly necessary if Daemonic mode was enabled but should was done if possible         scheduler.shutdown ()         print (' Exit the job! ')

Using cron to schedule a blocking approach

 1 # coding=utf-8 2 "" "3 demonstrates how to use the background scheduler to schedule a job that executes on 3 second 4 I Ntervals. 5 "" "6 7 from datetime import datetime 8 import OS 9 from apscheduler.schedulers.blocking import BlockingScheduler11 def tick (): Print (' tick! The time is:%s '% DateTime.Now ()) If __name__ = = ' __main__ ': Scheduler = Blockingscheduler () schedule R.add_job (tick, ' cron ', day_of_week= ' 6 ', second= ' */5 ') "(INT|STR) –4-digit year22 Month ( INT|STR) –month (1-12) (INT|STR) –day of the (1-31) week (INT|STR)-ISO week (1-53)-D Ay_of_week (INT|STR) –number or name of weekday (0-6 or Mon,tue,wed,thu,fri,sat,sun) hour (INT|STR) –hour (0-2 3) 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) all fire on every value35 * * A any fire every a values, starting from the MINIMUM36 A-B any fire in any value within the-a range (a must be smaller than B) PNs a-b/c any fire every C values within the A-a range38 xth y day Fi  Re on the x-th occurrence of weekday y within the month39 last X day fire in the last occurrence of weekday x within the MONTH40 last day fire on the last day within the month41 z/y Atching expression; Can combine any number of any of the the above Expressions42 "" on the "" Press ctrl+{0} to exit ". Format (' Break ' If Os.name = = ' nt ' Else ' C ') (try:47 Scheduler.start () #采用的是阻塞的方式, only one thread dedicated to scheduling tasks except (Keybo ArdINterrupt, Systemexit): $ # Not strictly necessary if Daemonic mode was enabled but should are done if possible50 Scheduler.shutdown () print (' Exit the job! ')

Python scheduling framework Apscheduler using the detailed

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.