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, ' interval ', seconds=3) #间隔3秒钟执行一次 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 Kee PS 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, ' Interval ', seconds=3) scheduler.add_job (tick, ' Date ', run_date= ' 2016-02-14 15:01:05 ') #在指定的时间, execute only once Scheduler.start ( #这里的调度任务是独立的一个线程 print (' Press ctrl+{0} to exit '. Format (' break ' if os.name = = ' nt ' Else ' C ') Try: # This is here T o 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, ' Interval ', 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 ') "' Year (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,thu,fri,sat,sun) hour (INT|STR) –hour (0-23) minute (int|str) –minute (0-59) second (INT|STR) –sec Ond (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 CALCU Lations (defaults to scheduler timezone) * All fire in every value */a any fire every a values, starting from The minimum-A-a-a-fire in any value within the-a-range (a must be smaller than B) a-b/c all fire every C Val UEs within the A-B range xth y day fire on the x-th occurrence of weekday y within the month last X The last occurrence of weekday x within the month last day fire on the last day within the month x, y, z any fire on 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.shut Down () 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, ' interval ', 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, ' Date ', run_date= ' 2016-02-14 15:23:05 ') print (' Press ctrl+{0} to exit '. Format (' break ' if Os.name = ' nt ' Else ' C ') try: scheduler.start () #采用的是阻塞的方式, with only one thread dedicated to scheduling tasks except (Keyboardinterrupt, Systemexit): # strictly necessary if Daemonic mode is enabled but should being done if possible scheduler.shutdown () pri NT (' Exit the 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, ' CR On ', day_of_week= ' 6 ', second= ' */5 ') "Year" (int|str) –4-digit Year Month (INT|STR) –month (1-12) Day (int|st R) –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 MO N,tue,wed,thu,fri,sat,sun) hour (INT|STR) –hour (0-23) minute (int|str) –minute (0-59) second (INT|STR) –secon D (0-59) start_date (DATETIME|STR) –earliest possible date/time to trigger on (inclusive) end_date (datetime|st R) –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 Minimu M-a any fire on any value within the-a-range (a must be smaller than B) a-b/c all fire every C values within The A-B range xth y day fire on the x-th occurrence of weekday y within the month last X day fire in the last Occurrence of weekday x within the month last day fire on the last day within the month x, y, z any fire in any MA Tching expression; 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! ')