python中BackgroundScheduler和BlockingScheduler的區(qū)別
APScheduler最基本的用法: “定時(shí)幾秒后啟動(dòng)job”
兩種調(diào)度器: BackgroundScheduler和BlockingScheduler的區(qū)別,
job執(zhí)行時(shí)間大于定時(shí)調(diào)度時(shí)間特殊情況的問(wèn)題及解決方法
每個(gè)job都會(huì)以thread的方式被調(diào)度。
1、基本的定時(shí)調(diào)度
APScheduler是python的一個(gè)定時(shí)任務(wù)調(diào)度框架,能實(shí)現(xiàn)類似linux下crontab類型的任務(wù),使用起來(lái)比較方便。它提供基于固定時(shí)間間隔、日期以及crontab配置類似的任務(wù)調(diào)度,并可以持久化任務(wù),或?qū)⑷蝿?wù)以daemon方式運(yùn)行。
下面是一個(gè)最基本的使用示例:
from apscheduler.schedulers.blocking import BlockingScheduler def job(): print('job 3s') if __name__=='__main__': sched = BlockingScheduler(timezone='MST') sched.add_job(job, 'interval', id='3_second_job', seconds=3) sched.start()
它能實(shí)現(xiàn)每隔3s就調(diào)度job()運(yùn)行一次,所以程序每隔3s就輸出'job 3s'。通過(guò)修改add_job()的參數(shù)seconds,就可以改變?nèi)蝿?wù)調(diào)度的間隔時(shí)間。
2、BlockingScheduler與BackgroundScheduler區(qū)別
APScheduler中有很多種不同類型的調(diào)度器,BlockingScheduler與BackgroundScheduler是其中最常用的兩種調(diào)度器。那他們之間有什么區(qū)別呢? 簡(jiǎn)單來(lái)說(shuō),區(qū)別主要在于BlockingScheduler會(huì)阻塞主線程的運(yùn)行,而B(niǎo)ackgroundScheduler不會(huì)阻塞。所以,我們?cè)诓煌那闆r下,選擇不同的調(diào)度器:
BlockingScheduler: 調(diào)用start函數(shù)后會(huì)阻塞當(dāng)前線程。當(dāng)調(diào)度器是你應(yīng)用中唯一要運(yùn)行的東西時(shí)(如上例)使用。
BackgroundScheduler: 調(diào)用start后主線程不會(huì)阻塞。當(dāng)你不運(yùn)行任何其他框架時(shí)使用,并希望調(diào)度器在你應(yīng)用的后臺(tái)執(zhí)行。
下面用兩個(gè)例子來(lái)更直觀的說(shuō)明兩者的區(qū)別。
BlockingScheduler例子
from apscheduler.schedulers.blocking import BlockingScheduler import time def job(): print('job 3s') if __name__=='__main__': sched = BlockingScheduler(timezone='MST') sched.add_job(job, 'interval', id='3_second_job', seconds=3) sched.start() while(True): # 不會(huì)被執(zhí)行到 print('main 1s') time.sleep(1)
運(yùn)行這個(gè)程序,我們得到如下的輸出:
job 3s
job 3s
job 3s
job 3s
可見(jiàn),BlockingScheduler調(diào)用start函數(shù)后會(huì)阻塞當(dāng)前線程,導(dǎo)致主程序中while循環(huán)不會(huì)被執(zhí)行到。
BackgroundScheduler例子
from apscheduler.schedulers.background import BackgroundScheduler import time def job(): print('job 3s') if __name__=='__main__': sched = BackgroundScheduler(timezone='MST') sched.add_job(job, 'interval', id='3_second_job', seconds=3) sched.start() while(True): print('main 1s') time.sleep(1)
可見(jiàn),BackgroundScheduler調(diào)用start函數(shù)后并不會(huì)阻塞當(dāng)前線程,所以可以繼續(xù)執(zhí)行主程序中while循環(huán)的邏輯。
main 1s
main 1s
main 1s
job 3s
main 1s
main 1s
main 1s
job 3s
通過(guò)這個(gè)輸出,我們也可以發(fā)現(xiàn),調(diào)用start函數(shù)后,job()并不會(huì)立即開(kāi)始執(zhí)行。而是等待3s后,才會(huì)被調(diào)度執(zhí)行。
如何讓job在start()后就開(kāi)始運(yùn)行
如何才能讓調(diào)度器調(diào)用start函數(shù)后,job()就立即開(kāi)始執(zhí)行呢?
其實(shí)APScheduler并沒(méi)有提供很好的方法來(lái)解決這個(gè)問(wèn)題,但有一種最簡(jiǎn)單的方式,就是在調(diào)度器start之前,就運(yùn)行一次job(),如下
from apscheduler.schedulers.background import BackgroundScheduler import time def job(): print('job 3s') if __name__=='__main__': job() # 執(zhí)行一次就好了喲 sched = BackgroundScheduler(timezone='MST') sched.add_job(job, 'interval', id='3_second_job', seconds=3) sched.start() while(True): print('main 1s') time.sleep(1)
這樣就能得到如下的輸出
job 3s
main 1s
main 1s
main 1s
job 3s
main 1s
main 1s
main 1s
這樣雖然沒(méi)有絕對(duì)做到“讓job在start()后就開(kāi)始運(yùn)行”,但也能做到“不等待調(diào)度,而是剛開(kāi)始就運(yùn)行job”。
如果job執(zhí)行時(shí)間過(guò)長(zhǎng)會(huì)怎么樣
如果執(zhí)行job()的時(shí)間需要5s,但調(diào)度器配置為每隔3s就調(diào)用一下job(),會(huì)發(fā)生什么情況呢?我們寫了如下例子:
from apscheduler.schedulers.background import BackgroundScheduler import time def job(): print('job 3s') time.sleep(5) if __name__=='__main__': sched = BackgroundScheduler(timezone='MST') sched.add_job(job, 'interval', id='3_second_job', seconds=3) sched.start() while(True): print('main 1s') time.sleep(1)
運(yùn)行這個(gè)程序,我們得到如下的輸出:
main 1s
main 1s
main 1s
job 3s
main 1s
main 1s
main 1s
Execution of job "job (trigger: interval[0:00:03], next run at: 2018-05-07 02:44:29 MST)" skipped: maximum number of running instances reached (1)
main 1s
main 1s
main 1s
job 3s
main 1s
可見(jiàn),3s時(shí)間到達(dá)后,并不會(huì)“重新啟動(dòng)一個(gè)job線程”,而是會(huì)跳過(guò)該次調(diào)度,等到下一個(gè)周期(再等待3s),又重新調(diào)度job()。
為了能讓多個(gè)job()同時(shí)運(yùn)行,我們也可以配置調(diào)度器的參數(shù)max_instances,如下例,我們?cè)试S2個(gè)job()同時(shí)運(yùn)行:
from apscheduler.schedulers.background import BackgroundScheduler import time def job(): print('job 3s') time.sleep(5) if __name__=='__main__': job_defaults = { 'max_instances': 2 } sched = BackgroundScheduler(timezone='MST', job_defaults=job_defaults) sched.add_job(job, 'interval', id='3_second_job', seconds=3) sched.start() while(True): print('main 1s') time.sleep(1)
運(yùn)行程序,我們得到如下的輸出:
main 1s
main 1s
main 1s
job 3s
main 1s
main 1s
main 1s
job 3s
main 1s
main 1s
main 1s
job 3s
每個(gè)job是怎么被調(diào)度的
通過(guò)上面的例子,我們發(fā)現(xiàn),調(diào)度器是定時(shí)調(diào)度job()函數(shù),來(lái)實(shí)現(xiàn)調(diào)度的。
那job()函數(shù)會(huì)被以進(jìn)程的方式調(diào)度運(yùn)行,還是以線程來(lái)運(yùn)行呢?
為了弄清這個(gè)問(wèn)題,我們寫了如下程序:
from apscheduler.schedulers.background import BackgroundScheduler import time,os,threading def job(): print('job thread_id-{0}, process_id-{1}'.format(threading.get_ident(), os.getpid())) time.sleep(50) if __name__=='__main__': job_defaults = { 'max_instances': 20 } sched = BackgroundScheduler(timezone='MST', job_defaults=job_defaults) sched.add_job(job, 'interval', id='3_second_job', seconds=3) sched.start() while(True): print('main 1s') time.sleep(1)
運(yùn)行程序,我們得到如下的輸出:
main 1s
main 1s
main 1s
job thread_id-10644, process_id-8872
main 1s
main 1s
main 1s
job thread_id-3024, process_id-8872
main 1s
main 1s
main 1s
job thread_id-6728, process_id-8872
main 1s
main 1s
main 1s
job thread_id-11716, process_id-8872
可見(jiàn),每個(gè)job()的進(jìn)程ID都相同,但線程ID不同。所以,job()最終是以線程的方式被調(diào)度執(zhí)行。
到此這篇關(guān)于python中BackgroundScheduler和BlockingScheduler的區(qū)別 的文章就介紹到這了,更多相關(guān)python BackgroundScheduler BlockingScheduler內(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)站,禁止在非www.sddonglingsh.com所屬的服務(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處理。