python调度框架APScheduler使用详解

来源:互联网 发布:程序员接私活app 编辑:程序博客网 时间:2024/04/29 16:04

python调度框架APScheduler使用详解:

from:http://www.cnblogs.com/hushaojun/p/5189109.html

复制代码
 1 # coding=utf-8 2 """ 3 Demonstrates how to use the background scheduler to schedule a job that executes on 3 second 4 intervals. 5 """ 6  7 from datetime import datetime 8 import time 9 import os10 11 from apscheduler.schedulers.background import BackgroundScheduler12 13 14 def tick():15     print('Tick! The time is: %s' % datetime.now())16 17 18 if __name__ == '__main__':19     scheduler = BackgroundScheduler()20     scheduler.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'))23 24     try:25         # This is here to simulate application activity (which keeps the main thread alive).26         while True:27             time.sleep(2)    #其他任务是独立的线程执行28             print('sleep!')29     except (KeyboardInterrupt, SystemExit):30         # Not strictly necessary if daemonic mode is enabled but should be done if possible31         scheduler.shutdown()32         print('Exit The Job!')
复制代码

 

非阻塞调度,在指定的时间执行一次

复制代码
 1 # coding=utf-8 2 """ 3 Demonstrates how to use the background scheduler to schedule a job that executes on 3 second 4 intervals. 5 """ 6  7 from datetime import datetime 8 import time 9 import os10 11 from apscheduler.schedulers.background import BackgroundScheduler12 13 14 def tick():15     print('Tick! The time is: %s' % datetime.now())16 17 18 if __name__ == '__main__':19     scheduler = BackgroundScheduler()20     #scheduler.add_job(tick, 'interval', seconds=3)21     scheduler.add_job(tick, 'date', run_date='2016-02-14 15:01:05')  #在指定的时间,只执行一次22     scheduler.start()    #这里的调度任务是独立的一个线程23     print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))24 25     try:26         # This is here to simulate application activity (which keeps the main thread alive).27         while True:28             time.sleep(2)    #其他任务是独立的线程执行29             print('sleep!')30     except (KeyboardInterrupt, SystemExit):31         # Not strictly necessary if daemonic mode is enabled but should be done if possible32         scheduler.shutdown()33         print('Exit The Job!')
复制代码

 

非阻塞的方式,采用cron的方式执行

复制代码
 1 # coding=utf-8 2 """ 3 Demonstrates how to use the background scheduler to schedule a job that executes on 3 second 4 intervals. 5 """ 6  7 from datetime import datetime 8 import time 9 import os10 11 from apscheduler.schedulers.background import BackgroundScheduler12 13 14 def tick():15     print('Tick! The time is: %s' % datetime.now())16 17 18 if __name__ == '__main__':19     scheduler = BackgroundScheduler()20     #scheduler.add_job(tick, 'interval', seconds=3)21     #scheduler.add_job(tick, 'date', run_date='2016-02-14 15:01:05')22     scheduler.add_job(tick, 'cron', day_of_week='6', second='*/5')23     '''24         year (int|str) – 4-digit year25         month (int|str) – month (1-12)26         day (int|str) – day of the (1-31)27         week (int|str) – ISO week (1-53)28         day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)29         hour (int|str) – hour (0-23)30         minute (int|str) – minute (0-59)31         second (int|str) – second (0-59)32         33         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)35         timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)36     37         *    any    Fire on every value38         */a    any    Fire every a values, starting from the minimum39         a-b    any    Fire on any value within the a-b range (a must be smaller than b)40         a-b/c    any    Fire every c values within the a-b range41         xth y    day    Fire on the x -th occurrence of weekday y within the month42         last x    day    Fire on the last occurrence of weekday x within the month43         last    day    Fire on the last day within the month44         x,y,z    any    Fire on any matching expression; can combine any number of any of the above expressions45     '''46     scheduler.start()    #这里的调度任务是独立的一个线程47     print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))48 49     try:50         # This is here to simulate application activity (which keeps the main thread alive).51         while True:52             time.sleep(2)    #其他任务是独立的线程执行53             print('sleep!')54     except (KeyboardInterrupt, SystemExit):55         # Not strictly necessary if daemonic mode is enabled but should be done if possible56         scheduler.shutdown()57         print('Exit The Job!')
复制代码

 

阻塞的方式,间隔3秒执行一次

复制代码
 1 # coding=utf-8 2 """ 3 Demonstrates how to use the background scheduler to schedule a job that executes on 3 second 4 intervals. 5 """ 6  7 from datetime import datetime 8 import os 9 10 from apscheduler.schedulers.blocking import BlockingScheduler11 12 13 def tick():14     print('Tick! The time is: %s' % datetime.now())15 16 17 if __name__ == '__main__':18     scheduler = BlockingScheduler()19     scheduler.add_job(tick, 'interval', seconds=3)20     21     print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))22 23     try:24         scheduler.start()    #采用的是阻塞的方式,只有一个线程专职做调度的任务25     except (KeyboardInterrupt, SystemExit):26         # Not strictly necessary if daemonic mode is enabled but should be done if possible27         scheduler.shutdown()28         print('Exit The Job!')
复制代码

 

采用阻塞的方法,只执行一次

复制代码
 1 # coding=utf-8 2 """ 3 Demonstrates how to use the background scheduler to schedule a job that executes on 3 second 4 intervals. 5 """ 6  7 from datetime import datetime 8 import os 9 10 from apscheduler.schedulers.blocking import BlockingScheduler11 12 13 def tick():14     print('Tick! The time is: %s' % datetime.now())15 16 17 if __name__ == '__main__':18     scheduler = BlockingScheduler()19     scheduler.add_job(tick, 'date', run_date='2016-02-14 15:23:05')20     21     print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))22 23     try:24         scheduler.start()    #采用的是阻塞的方式,只有一个线程专职做调度的任务25     except (KeyboardInterrupt, SystemExit):26         # Not strictly necessary if daemonic mode is enabled but should be done if possible27         scheduler.shutdown()28         print('Exit The Job!')
复制代码

 

采用阻塞的方式,使用cron的调度方法

复制代码
 1 # coding=utf-8 2 """ 3 Demonstrates how to use the background scheduler to schedule a job that executes on 3 second 4 intervals. 5 """ 6  7 from datetime import datetime 8 import os 9 10 from apscheduler.schedulers.blocking import BlockingScheduler11 12 13 def tick():14     print('Tick! The time is: %s' % datetime.now())15 16 17 if __name__ == '__main__':18     scheduler = BlockingScheduler()19     scheduler.add_job(tick, 'cron', day_of_week='6', second='*/5')20     '''21         year (int|str) – 4-digit year22         month (int|str) – month (1-12)23         day (int|str) – day of the (1-31)24         week (int|str) – ISO week (1-53)25         day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)26         hour (int|str) – hour (0-23)27         minute (int|str) – minute (0-59)28         second (int|str) – second (0-59)29         30         start_date (datetime|str) – earliest possible date/time to trigger on (inclusive)31         end_date (datetime|str) – latest possible date/time to trigger on (inclusive)32         timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)33     34         *    any    Fire on every value35         */a    any    Fire every a values, starting from the minimum36         a-b    any    Fire on any value within the a-b range (a must be smaller than b)37         a-b/c    any    Fire every c values within the a-b range38         xth y    day    Fire on the x -th occurrence of weekday y within the month39         last x    day    Fire on the last occurrence of weekday x within the month40         last    day    Fire on the last day within the month41         x,y,z    any    Fire on any matching expression; can combine any number of any of the above expressions42     '''43     44     print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))45 46     try:47         scheduler.start()    #采用的是阻塞的方式,只有一个线程专职做调度的任务48     except (KeyboardInterrupt, SystemExit):49         # Not strictly necessary if daemonic mode is enabled but should be done if possible50         scheduler.shutdown()51         print('Exit The Job!')
复制代码

0 0