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)
安裝依賴(lài):
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)前線(xiàn)程,不能立即返回。 BackgroundScheduler:適用于調(diào)度程序在應(yīng)用程序的后臺(tái)運(yùn)行,調(diào)用start后主線(xiàn)程不會(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)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信