Framework Apscheduler An example of how a schedule is used in Python

Source: Internet
Author: User
This article mainly introduces the detailed Python scheduling framework Apscheduler use, small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.

Recently in the study of Python scheduling framework Apscheduler use of the road, then today is a study notes it!

# Coding=utf-8 "" "demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals." "From datetime import datetimeimport timeimport osfrom apscheduler.schedulers.background Import backgroundschedulerdef Tick ():  print (' tick! The time is:%s '% DateTime.Now ()) If name = = ' main ':  scheduler = Backgroundscheduler ()  scheduler.add_job (tick, ' I Nterval ', seconds=3) #间隔3秒钟执行一次  scheduler.start ()  #这里的调度任务是独立的一个线程  print (' Press ctrl+{0} to exit '.  Format (' break ' if os.name = = ' nt ' Else ', ' C ')) Try: # This was here to    simulate application activity (which keeps the Main thread alive).    While True:      time.sleep (2)  #其他任务是独立的线程执行      print (' sleep! ')  Except (Keyboardinterrupt, Systemexit):    # Not strictly necessary if Daemonic mode was enabled but should being done if PO Ssible    scheduler.shutdown ()    print (' Exit the job! ')

Non-blocking scheduling, executed once at a specified time

# coding=utf-8 "" "demonstrates how to use the background scheduler to schedule a job that executes O N 3 secondintervals. "" " From datetime import datetimeimport timeimport osfrom apscheduler.schedulers.background Import backgroundschedulerdef Tick (): Print (' tick! The time is:%s '% DateTime.Now ()) If name = = ' main ': Scheduler = Backgroundscheduler () #scheduler. Add_job (tick, ' Interva L ', seconds=3) scheduler.add_job (tick, ' Date ', run_date= ' 2016-02-14 15:01:05 ') #在指定的时间, execute only once Scheduler.start () #这里的调度任  The service is independent of a thread print (' press ctrl+{0} to exit '. Format (' break ' if os.name = = ' nt ' Else ' C ')) Try: # This was here to simulate    Application activity (which keeps the main thread alive).  While True:time.sleep (2) #其他任务是独立的线程执行 print (' sleep! ') Except (Keyboardinterrupt, systemexit): # Not strictly necessary if Daemonic mode was enabled but should being done if poss Ible Scheduler.shutdown () print (' Exit the job! ') 

Non-blocking, cron-style execution

# Coding=utf-8 "" "demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals." "From datetime import datetimeimport timeimport osfrom apscheduler.schedulers.background Import backgroundschedulerdef Tick (): Print (' tick! The time is:%s '% DateTime.Now ()) If name = = ' main ': Scheduler = Backgroundscheduler () #scheduler. Add_job (tick, ' Interva L ', seconds=3) #scheduler. Add_job (tick, ' Date ', run_date= ' 2016-02-14 15:01:05 ') scheduler.add_job (tick, ' cron ', day_of  _week= ' 6 ', second= ' */5 ') ' (int|str) –4-digit Year Month (INT|STR) –month (1-12) Day (int|str) –day of The (1-31) week (INT|STR), ISO week (1-53) Day_of_week (int|str) –number or name of weekday (0-6 or mon,tue,wed,t        Hu,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 in (inclusive) timezone (DATETIME.TZINFO|STR) –time zone to use for the Date/time calculations (Defaults to Scheduler timezone) * No fire in every value */a any fire every a values, starting from the Mini Mum A-B all fire on any value within the-a-range (a must be smaller than B) a-b/c all fire every C values with In the A-B range xth y day fire on the x-th occurrence of weekday y within the month last X Day fire on the Las T occurrence of weekday x within the month last day fire on the last day within the month x, Y, z any matching expression;  Can combine any number of any of the above Expressions "Scheduler.start () #这里的调度任务是独立的一个线程 print (' Press ctrl+{0} to Exit '. Format (' break ' if os.name = = ' nt ' Else ' C ')) Try: # This is the here to simulate application activity (which keeps    The main thread alive).  While True:time.sleep (2) #其他任务是独立的线程执行 print (' sleep! ') Except (Keyboardinterrupt, SysTemexit): # Not strictly necessary if Daemonic mode was enabled but should being done if possible scheduler.shutdown () Print (' Exit the job! ')

Blocked way, 3 seconds interval execution

# Coding=utf-8 "" "demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals." "From datetime import datetimeimport osfrom apscheduler.schedulers.blocking import Blockingschedulerdef tick ():  Print (' tick! The time is:%s '% DateTime.Now ()) If name = = ' main ':  scheduler = Blockingscheduler ()  scheduler.add_job (tick, ' int Erval ', seconds=3)    print (' Press ctrl+{0} to exit '. Format (' break ' if os.name = = ' nt ' Else ' C '))  try:    Scheduler.start ()  #采用的是阻塞的方式, only one thread dedicated to scheduling tasks  except (Keyboardinterrupt, Systemexit):    # not strictly Necessary if Daemonic mode is enabled but should are done if possible    scheduler.shutdown ()    print (' Exit the job! ')

Use a blocking method to perform only once

# Coding=utf-8 "" "demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals." "From datetime import datetimeimport osfrom apscheduler.schedulers.blocking import Blockingschedulerdef tick ():  Print (' tick! The time is:%s '% DateTime.Now ()) If name = = ' main ':  scheduler = Blockingscheduler ()  scheduler.add_job (tick, ' Dat E ', run_date= ' 2016-02-14 15:23:05 ')  print (' Press ctrl+{0} to exit '. Format (' break ' if Os.name = ' nt ' Else ' C '))  t Ry:    scheduler.start ()  #采用的是阻塞的方式, only one thread dedicated to scheduling tasks  except (Keyboardinterrupt, Systemexit):    # not Strictly necessary if Daemonic mode is enabled but should are done if possible    scheduler.shutdown ()    print (' Exit Th E job! ')

Using cron to schedule a blocking approach

# Coding=utf-8 "" "demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals." "From datetime import datetimeimport osfrom apscheduler.schedulers.blocking import Blockingschedulerdef tick (): Print (' tick! The time is:%s '% DateTime.Now ()) If name = = ' main ': Scheduler = Blockingscheduler () scheduler.add_job (tick, ' cron ', day  _of_week= ' 6 ', second= ' */5 ') ' (int|str) –4-digit Year Month (INT|STR) –month (1-12) Day (INT|STR) –day of the (1-31) week (INT|STR), ISO week (1-53) Day_of_week (int|str) –number or name of weekday (0-6 or Mon,tue,we         D,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) –lat EST possible date/time to trigger on (inclusive) timezone (DATETIME.TZINFO|STR) –time zone to use for the Date/time CA Lculations (Defaultsto scheduler TimeZone) * No fire in every value */a any fire every a values, starting from the minimum A-B  Any fire on any value within the A-B range (a must be smaller than B. a-b/c any fire every C values within the A-D Range Xth y day fire in the x-th occurrence of weekday y within the month last X day fire in the last Occurren Ce of Weekday x within the month last day fire on the last day within the month x, y, z any fire in any matching E Xpression  Can combine any number of any of the above expressions "print (' press ctrl+{0} to exit '. Format (' break ' if os.name = = ' NT ' Else ' C ') Try:scheduler.start () #采用的是阻塞的方式, only one thread dedicated to scheduling tasks except (Keyboardinterrupt, systemexit): # not S trictly necessary if Daemonic mode is enabled but should are done if possible scheduler.shutdown () print (' Exit the Jo b! ')
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.