Python定時(shí)庫(kù)Apscheduler的簡(jiǎn)單使用
在Python中需要執(zhí)行定時(shí)任務(wù),可以使用Apscheduler。
Apscheduler是基于Quartz的Python定時(shí)任務(wù)框架,功能上跟Quartz一致,使用上跟Quartz也幾乎一致。
核心的四個(gè)部分:
①觸發(fā)器(trigger)、②作業(yè)存儲(chǔ)(job store)、③執(zhí)行器(executor)、④調(diào)度器(scheduler)
安裝依賴:
pip install apscheduler
間隔時(shí)間調(diào)度:
from apscheduler.schedulers.blocking import BlockingScheduler from datetime import datetime sched = BlockingScheduler() def test_job(): print(f'{datetime.now():%H:%M:%S} Test job') if __name__ == '__main__': sched.add_job(test_job, 'interval', id='test', seconds=5) sched.start()
也可以使用裝飾器:
from apscheduler.schedulers.blocking import BlockingScheduler from datetime import datetime sched = BlockingScheduler() def test_job(): print(f'{datetime.now():%H:%M:%S} Test job') @sched.scheduled_job('interval', seconds=5) def test_decorator_job(): print(f'{datetime.now():%H:%M:%S} Test decorator job') if __name__ == '__main__': sched.add_job(test_job, 'interval', id='test', seconds=5) sched.start()
運(yùn)行結(jié)果:
BlockingScheduler()是調(diào)度器中的一種調(diào)度器
sched.add_job()是添加作業(yè)
sched.start()是開(kāi)始任務(wù)
定時(shí)調(diào)度:
定時(shí)調(diào)度使用cron表達(dá)式進(jìn)行,這里也帶參數(shù)執(zhí)行:
from apscheduler.schedulers.blocking import BlockingScheduler from datetime import datetime scheduler = BlockingScheduler() def test_args(x): print (f'{datetime.now():%H:%M:%S} Test cron job', x) if __name__ == '__main__': scheduler.add_job(test_args, 'cron', args=('cron test',), second='*/5') scheduler.start()
時(shí)間參數(shù)設(shè)置如下:
year (int|str) – 年,4位數(shù)字 month (int|str) – 月 (范圍1-12) day (int|str) – 日 (范圍1-31) week (int|str) – 周 (范圍1-53) day_of_week (int|str) – 周內(nèi)第幾天或者星期幾 (范圍0-6 或者 mon,tue,wed,thu,fri,sat,sun) hour (int|str) – 時(shí) (范圍0-23) minute (int|str) – 分 (范圍0-59) second (int|str) – 秒 (范圍0-59) start_date (datetime|str) – 最早開(kāi)始日期(包含) end_date (datetime|str) – 最晚結(jié)束時(shí)間(包含) timezone (datetime.tzinfo|str) – 指定時(shí)區(qū)
接下來(lái)說(shuō)一下其中的調(diào)度器:
BlockingScheduler:適用于調(diào)度程序是進(jìn)程中唯一運(yùn)行的進(jìn)程,調(diào)用start函數(shù)會(huì)阻塞當(dāng)前線程,不能立即返回。 BackgroundScheduler:適用于調(diào)度程序在應(yīng)用程序的后臺(tái)運(yùn)行,調(diào)用start后主線程不會(huì)阻塞。 AsyncIOScheduler:適用于使用了asyncio模塊的應(yīng)用程序。 GeventScheduler:適用于使用gevent模塊的應(yīng)用程序。 TwistedScheduler:適用于構(gòu)建Twisted的應(yīng)用程序。 QtScheduler:適用于構(gòu)建Qt的應(yīng)用程序。
其中使用得比較多的是前三種調(diào)度器。
刪除任務(wù):
scheduler.remove_job('task_id')
停止任務(wù):
scheduler.pause_job('task_id')
恢復(fù)任務(wù):
scheduler.resume_job('task_id')
立即執(zhí)行任務(wù)next_run_time:
scheduler.add_job( test_job, 'interval', minutes=5, next_run_time=datetime.datetime.now() )
到此這篇關(guān)于Python定時(shí)庫(kù)Apscheduler的簡(jiǎn)單使用的文章就介紹到這了,更多相關(guān)Python定時(shí)庫(kù)Apscheduler使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python APScheduler執(zhí)行使用方法詳解
- python 基于Apscheduler實(shí)現(xiàn)定時(shí)任務(wù)
- python定時(shí)任務(wù)apscheduler的詳細(xì)使用教程
- python?包之?APScheduler?定時(shí)任務(wù)
- 最新Python?APScheduler?定時(shí)任務(wù)詳解
- Python flask框架定時(shí)任務(wù)apscheduler應(yīng)用介紹
- Python實(shí)現(xiàn)定時(shí)任務(wù)利器之a(chǎn)pscheduler使用詳解
- Python高效定時(shí)任務(wù)處理APScheduler庫(kù)深入學(xué)習(xí)
- Python apscheduler實(shí)現(xiàn)定時(shí)任務(wù)的方法詳解
- Python輕量級(jí)定時(shí)任務(wù)調(diào)度APScheduler的使用
相關(guān)文章
解決python 輸出到csv 出現(xiàn)多空行的情況
這篇文章主要介紹了解決python 輸出到csv 出現(xiàn)多空行的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03python?aeon庫(kù)進(jìn)行時(shí)間序列算法預(yù)測(cè)分類實(shí)例探索
這篇文章主要介紹了python?aeon庫(kù)進(jìn)行時(shí)間序列算法預(yù)測(cè)分類實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-02-02如何基于Python實(shí)現(xiàn)電子郵件的發(fā)送
這篇文章主要介紹了如何基于Python實(shí)現(xiàn)電子郵件的發(fā)送,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12NumPy對(duì)數(shù)組按索引查詢實(shí)戰(zhàn)方法總結(jié)
數(shù)組的高級(jí)操作主要是組合數(shù)組,拆分?jǐn)?shù)組,tile數(shù)組和重組元素,下面這篇文章主要給大家介紹了關(guān)于NumPy對(duì)數(shù)組按索引查詢的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08python的三目運(yùn)算符和not in運(yùn)算符使用示例
這篇文章主要介紹了python的三目運(yùn)算符和not in運(yùn)算符使用示例,需要的朋友可以參考下2014-03-03matplotlib交互式數(shù)據(jù)光標(biāo)實(shí)現(xiàn)(mplcursors)
這篇文章主要介紹了matplotlib交互式數(shù)據(jù)光標(biāo)實(shí)現(xiàn)(mplcursors),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01python3多線程知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給各位分享的是關(guān)于python3多線程的相關(guān)知識(shí)點(diǎn)內(nèi)容,以后需要的朋友們可以參考下。2019-09-09Python利用glob庫(kù)實(shí)現(xiàn)輕松應(yīng)對(duì)文件和目錄管理
Python提供了glob庫(kù),它允許我們根據(jù)特定模式匹配文件和目錄,本文將詳細(xì)介紹glob庫(kù)的用法,并通過(guò)實(shí)例演示它的各種功能,需要的可以了解一下2023-07-07