The python scheduling framework APScheduler, pythonapscheduler
I recently studied how to use the python scheduling framework APScheduler. Now, let's take a note!
# Coding = UTF-8 "" Demonstrates how to use the background schedto to schedule a job that executes on 3 secondintervals. "from datetime import datetimeimport timeimport osfrom apschedrom. 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) # execute schedval once every 3 seconds. start () # The scheduling task here is an independent thread print ('Press Ctrl + {0} to exit '. format ('Break' if OS. name = 'nt 'else 'C') try: # This is here to simulate application activity (which keeps the main thread alive ). while True: time. sleep (2) # other tasks are independent threads that execute print ( 'Sleep! ') Counter T (KeyboardInterrupt, SystemExit): # Not strictly necessary if daemonic mode is enabled but shocould be done if possible schedonic. shutdown () print ('Exit The Job! ')
Non-blocking scheduling, executed once at the specified time
# Coding = UTF-8 "" Demonstrates how to use the background schedto to schedule a job that executes on 3 secondintervals. "from datetime import datetimeimport timeimport osfrom apschedrom. 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) schedval. add_job (tick, 'date', run_date = '2017-02-14 15:01:05 ') # at the specified time, only scheduler is executed once. start () # The scheduling task here is an independent thread print ('Press Ctrl + {0} to exit '. format ('Break' if OS. name = 'nt 'else 'C') try: # This is here to simulate application activity (which keeps th E main thread alive). while True: time. sleep (2) # other tasks run print ('sleep! ') Counter T (KeyboardInterrupt, SystemExit): # Not strictly necessary if daemonic mode is enabled but shocould be done if possible schedonic. shutdown () print ('Exit The Job! ')
Non-blocking mode. cron is used for execution.
# Coding = UTF-8 "" Demonstrates how to use the background schedto to schedule a job that executes on 3 secondintervals. "from datetime import datetimeimport timeimport osfrom apschedrom. 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) # schedval. add_job (tick, 'date', run_date = '2017-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-5 3) 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 (random SIVE) end_date (datetime | str)-latest possible date/time to trigger on (random SIVE) timezone (datetime. tzinfo | str)-time zone to use for t He date/time calculations (defaults to scheduler timezone) * any Fire on every value */a any Fire every a values, starting from the minimum a-B any Fire on any value within the-B range (a must be smaller than B) a-B/c any Fire every c values within the-B range xth y day Fire on the x-th occurrence of weekday y within the month last x day Fire on the last occurrence of weekday x within the mon Th 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 ''' schedsions. start () # The scheduling task here is an independent thread print ('Press Ctrl + {0} to exit '. format ('Break' if OS. name = 'nt 'else 'C') try: # This is here to simulate application activity (which keeps the main thread alive ). while True: time. sleep (2) # other tasks are independent threads that execute print (' Sleep! ') Counter T (KeyboardInterrupt, SystemExit): # Not strictly necessary if daemonic mode is enabled but shocould be done if possible schedonic. shutdown () print ('Exit The Job! ')
Blocking method. The execution interval is 3 seconds.
# Coding = UTF-8 "" Demonstrates how to use the background schedto to schedule a job that executes on 3 secondintervals. "from datetime import datetimeimport osfrom apschedrom. 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 () # Use the blocking method. Only one thread is dedicated to the scheduling task worker T (KeyboardInterrupt, SystemExit ): # Not strictly necessary if daemonic mode is enabled but shoshould be done if possible schedble. s Hudown () print ('Exit The Job! ')
Use the blocking method and run it only once.
# Coding = UTF-8 "" Demonstrates how to use the background schedto to schedule a job that executes on 3 secondintervals. "from datetime import datetimeimport osfrom apschedrom. 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 = '2017-02-14 15:23:05 ') print ('Press Ctrl + {0} to exit '. format ('Break' if OS. name = 'nt 'else 'C') try: scheduler. start () # Use the blocking method. Only one thread is dedicated to the scheduling task worker T (KeyboardInterrupt, SystemExit ): # Not strictly necessary if daemonic mode is enabled but shoshould be done if pos Sible scheddown. shutdown () print ('Exit The Job! ')
Use the cron Scheduling Method in blocking mode
# Coding = UTF-8 "" Demonstrates how to use the background schedto to schedule a job that executes on 3 secondintervals. "from datetime import datetimeimport osfrom apschedrom. 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') ''' 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)-second (0-59) start_date (datetime | str) -earliest possible date/time to trigger on (random SIVE) end_date (datetime | str)-latest possible date/time to trigger on (random SIVE) timezone (datetime. tzinfo | str)-time zone to use for the date/time calculations (defaults to scheduler timezone) * any Fire on every value */a any Fire every a values, s Tarting from the minimum a-B any Fire on any value within the-B range (a must be smaller than B) a-B/c any Fire every c values within the-B range xth y day Fire on the x-th occurrence of weekday y within the month last x day Fire on 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 ''' print ('Press Ctrl + {0} to exit '. format ('Break' if OS. name = 'nt 'else 'C') try: scheduler. start () # Use the blocking method. Only one thread is dedicated to the scheduling task worker T (KeyboardInterrupt, SystemExit ): # Not strictly necessary if daemonic mode is enabled but shoshould be done if possible schedble. shutdown () print ('Exit The Job! ')
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.