This article describes how to use the python scheduling framework APScheduler. I think this is a good example. I will share it with you and give you a reference. Let's take a look at this article. This article describes how to use the python scheduling framework APScheduler. I think it is quite good. I will share it with you and give you a reference. Let's take a look at it with Xiaobian.
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 the main t Hread 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-53) day_o F_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, 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 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 ''' 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. shutdown ( ) 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 possible SC Hedworks. 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) minu Te (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, 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 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 a detailed description of the instances used by the framework APScheduler in python scheduling. For more information, see other related articles in the first PHP community!