Django timed task Python scheduling framework Apscheduler use

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 int Ervals. 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! ')

Django timed task Python scheduling framework Apscheduler use

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.