詳解python調(diào)度框架APScheduler使用
最近在研究python調(diào)度框架APScheduler使用的路上,那么今天也算個(gè)學(xué)習(xí)筆記吧!
# coding=utf-8 """ Demonstrates how to use the background scheduler to schedule a job that executes on 3 second intervals. """ from datetime import datetime import time import os from apscheduler.schedulers.background import BackgroundScheduler def tick(): print('Tick! The time is: %s' % datetime.now()) if __name__ == '__main__': scheduler = BackgroundScheduler() scheduler.add_job(tick, 'interval', seconds=3) #間隔3秒鐘執(zhí)行一次 scheduler.start() #這里的調(diào)度任務(wù)是獨(dú)立的一個(gè)線程 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) #其他任務(wù)是獨(dú)立的線程執(zhí)行 print('sleep!') except (KeyboardInterrupt, SystemExit): # Not strictly necessary if daemonic mode is enabled but should be done if possible scheduler.shutdown() print('Exit The Job!')
非阻塞調(diào)度,在指定的時(shí)間執(zhí)行一次
# coding=utf-8 """ Demonstrates how to use the background scheduler to schedule a job that executes on 3 second intervals. """ from datetime import datetime import time import os from apscheduler.schedulers.background import BackgroundScheduler def 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') #在指定的時(shí)間,只執(zhí)行一次 scheduler.start() #這里的調(diào)度任務(wù)是獨(dú)立的一個(gè)線程 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) #其他任務(wù)是獨(dú)立的線程執(zhí)行 print('sleep!') except (KeyboardInterrupt, SystemExit): # Not strictly necessary if daemonic mode is enabled but should be done if possible scheduler.shutdown() print('Exit The Job!')
非阻塞的方式,采用cron的方式執(zhí)行
# coding=utf-8 """ Demonstrates how to use the background scheduler to schedule a job that executes on 3 second intervals. """ from datetime import datetime import time import os from apscheduler.schedulers.background import BackgroundScheduler def 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) – 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 on (inclusive) 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 a-b range (a must be smaller than b) a-b/c any 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 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 ''' scheduler.start() #這里的調(diào)度任務(wù)是獨(dú)立的一個(gè)線程 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) #其他任務(wù)是獨(dú)立的線程執(zhí)行 print('sleep!') except (KeyboardInterrupt, SystemExit): # Not strictly necessary if daemonic mode is enabled but should be done if possible scheduler.shutdown() print('Exit The Job!')
阻塞的方式,間隔3秒執(zhí)行一次
# coding=utf-8 """ Demonstrates how to use the background scheduler to schedule a job that executes on 3 second intervals. """ from datetime import datetime import os from apscheduler.schedulers.blocking import BlockingScheduler def 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() #采用的是阻塞的方式,只有一個(gè)線程專職做調(diào)度的任務(wù) except (KeyboardInterrupt, SystemExit): # Not strictly necessary if daemonic mode is enabled but should be done if possible scheduler.shutdown() print('Exit The Job!')
采用阻塞的方法,只執(zhí)行一次
# coding=utf-8 """ Demonstrates how to use the background scheduler to schedule a job that executes on 3 second intervals. """ from datetime import datetime import os from apscheduler.schedulers.blocking import BlockingScheduler def 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() #采用的是阻塞的方式,只有一個(gè)線程專職做調(diào)度的任務(wù) except (KeyboardInterrupt, SystemExit): # Not strictly necessary if daemonic mode is enabled but should be done if possible scheduler.shutdown() print('Exit The Job!')
采用阻塞的方式,使用cron的調(diào)度方法
# coding=utf-8 """ Demonstrates how to use the background scheduler to schedule a job that executes on 3 second intervals. """ from datetime import datetime import os from apscheduler.schedulers.blocking import BlockingScheduler def 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 (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 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 a-b range (a must be smaller than b) a-b/c any 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 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() #采用的是阻塞的方式,只有一個(gè)線程專職做調(diào)度的任務(wù) except (KeyboardInterrupt, SystemExit): # Not strictly necessary if daemonic mode is enabled but should be done if possible scheduler.shutdown() print('Exit The Job!')
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python模擬登陸,用session維持回話的實(shí)例
今天小編就為大家分享一篇python模擬登陸,用session維持回話的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12python自動(dòng)化測試用例全對偶組合與全覆蓋組合比較
這篇文章主要為大家介紹了python自動(dòng)化測試用例全對偶組合與全覆蓋組合比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06利用Python?NumPy庫及Matplotlib庫繪制數(shù)學(xué)函數(shù)圖像
最近開始學(xué)習(xí)數(shù)學(xué)了,有一些題目的函數(shù)圖像非常有特點(diǎn),下面這篇文章主要給大家介紹了關(guān)于利用Python?NumPy庫及Matplotlib庫繪制數(shù)學(xué)函數(shù)圖像的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04使用Python腳本對GiteePages進(jìn)行一鍵部署的使用說明
剛好之前有了解過python的自動(dòng)化,就想著自動(dòng)化腳本,百度一搜還真有類似的文章。今天就給大家分享下使用Python腳本對GiteePages進(jìn)行一鍵部署的使用說明,感興趣的朋友一起看看吧2021-05-05用python的requests第三方模塊抓取王者榮耀所有英雄的皮膚實(shí)例
下面小編就為大家分享一篇用python的requests第三方模塊抓取王者榮耀所有英雄的皮膚實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨想過來看看吧2017-12-12Python利用ORM控制MongoDB(MongoEngine)的步驟全紀(jì)錄
MongoEngine是一個(gè)對象文檔映射器(ODM),相當(dāng)于一個(gè)基于SQL的對象關(guān)系映射器(ORM),下面這篇文章主要給大家介紹了關(guān)于Python利用ORM控制MongoDB(MongoEngine)的相關(guān)資料,需要的朋友可以參考下2018-09-09