溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python中BackgroundScheduler和BlockingScheduler的區(qū)別是什么

發(fā)布時間:2021-07-28 17:04:52 來源:億速云 閱讀:180 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“python中BackgroundScheduler和BlockingScheduler的區(qū)別是什么”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

目錄
  • 1、基本的定時調(diào)度

  • 2、BlockingScheduler與BackgroundScheduler區(qū)別

APScheduler最基本的用法: “定時幾秒后啟動job”
兩種調(diào)度器: BackgroundScheduler和BlockingScheduler的區(qū)別,
job執(zhí)行時間大于定時調(diào)度時間特殊情況的問題及解決方法
每個job都會以thread的方式被調(diào)度。

1、基本的定時調(diào)度

APScheduler是python的一個定時任務(wù)調(diào)度框架,能實現(xiàn)類似linux下crontab類型的任務(wù),使用起來比較方便。它提供基于固定時間間隔、日期以及crontab配置類似的任務(wù)調(diào)度,并可以持久化任務(wù),或?qū)⑷蝿?wù)以daemon方式運行。

下面是一個最基本的使用示例:

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()

它能實現(xiàn)每隔3s就調(diào)度job()運行一次,所以程序每隔3s就輸出'job 3s'。通過修改add_job()的參數(shù)seconds,就可以改變?nèi)蝿?wù)調(diào)度的間隔時間。

2、BlockingScheduler與BackgroundScheduler區(qū)別

APScheduler中有很多種不同類型的調(diào)度器,BlockingScheduler與BackgroundScheduler是其中最常用的兩種調(diào)度器。那他們之間有什么區(qū)別呢? 簡單來說,區(qū)別主要在于BlockingScheduler會阻塞主線程的運行,而BackgroundScheduler不會阻塞。所以,我們在不同的情況下,選擇不同的調(diào)度器:

BlockingScheduler: 調(diào)用start函數(shù)后會阻塞當前線程。當調(diào)度器是你應用中唯一要運行的東西時(如上例)使用。
BackgroundScheduler: 調(diào)用start后主線程不會阻塞。當你不運行任何其他框架時使用,并希望調(diào)度器在你應用的后臺執(zhí)行。
下面用兩個例子來更直觀的說明兩者的區(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): # 不會被執(zhí)行到
        print('main 1s')
        time.sleep(1)

運行這個程序,我們得到如下的輸出:

job 3s
job 3s
job 3s
job 3s 

可見,BlockingScheduler調(diào)用start函數(shù)后會阻塞當前線程,導致主程序中while循環(huán)不會被執(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)

可見,BackgroundScheduler調(diào)用start函數(shù)后并不會阻塞當前線程,所以可以繼續(xù)執(zhí)行主程序中while循環(huán)的邏輯。

main 1s
main 1s
main 1s
job 3s
main 1s
main 1s
main 1s
job 3s 

通過這個輸出,我們也可以發(fā)現(xiàn),調(diào)用start函數(shù)后,job()并不會立即開始執(zhí)行。而是等待3s后,才會被調(diào)度執(zhí)行。
如何讓job在start()后就開始運行
如何才能讓調(diào)度器調(diào)用start函數(shù)后,job()就立即開始執(zhí)行呢?

其實APScheduler并沒有提供很好的方法來解決這個問題,但有一種最簡單的方式,就是在調(diào)度器start之前,就運行一次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

這樣雖然沒有絕對做到“讓job在start()后就開始運行”,但也能做到“不等待調(diào)度,而是剛開始就運行job”。

如果job執(zhí)行時間過長會怎么樣
如果執(zhí)行job()的時間需要5s,但調(diào)度器配置為每隔3s就調(diào)用一下job(),會發(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)

運行這個程序,我們得到如下的輸出:

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

可見,3s時間到達后,并不會“重新啟動一個job線程”,而是會跳過該次調(diào)度,等到下一個周期(再等待3s),又重新調(diào)度job()。

為了能讓多個job()同時運行,我們也可以配置調(diào)度器的參數(shù)max_instances,如下例,我們允許2個job()同時運行:

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)

運行程序,我們得到如下的輸出:

main 1s
main 1s
main 1s
job 3s
main 1s
main 1s
main 1s
job 3s
main 1s
main 1s
main 1s
job 3s

每個job是怎么被調(diào)度的

通過上面的例子,我們發(fā)現(xiàn),調(diào)度器是定時調(diào)度job()函數(shù),來實現(xiàn)調(diào)度的。

那job()函數(shù)會被以進程的方式調(diào)度運行,還是以線程來運行呢?

為了弄清這個問題,我們寫了如下程序:

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)

運行程序,我們得到如下的輸出:

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

可見,每個job()的進程ID都相同,但線程ID不同。所以,job()最終是以線程的方式被調(diào)度執(zhí)行。

“python中BackgroundScheduler和BlockingScheduler的區(qū)別是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI