欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年07月28日 15:37:05   作者:屬于自己的世界  
這篇文章主要介紹了python中BackgroundScheduler和BlockingScheduler的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • pd.read_csv讀取文件路徑出現(xiàn)的問(wèn)題解決

    pd.read_csv讀取文件路徑出現(xiàn)的問(wèn)題解決

    本文主要介紹了pd.read_csv讀取文件路徑出現(xiàn)的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • python數(shù)據(jù)可視化之matplotlib.pyplot基礎(chǔ)以及折線圖

    python數(shù)據(jù)可視化之matplotlib.pyplot基礎(chǔ)以及折線圖

    不論是數(shù)據(jù)挖掘還是數(shù)據(jù)建模,都免不了數(shù)據(jù)可視化的問(wèn)題,對(duì)于Python來(lái)說(shuō),Matplotlib是最著名的繪圖庫(kù),它主要用于二維繪圖,這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)可視化之matplotlib.pyplot基礎(chǔ)以及折線圖的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • Python中matplotlib庫(kù)安裝失敗的經(jīng)驗(yàn)總結(jié)(附pycharm配置anaconda)

    Python中matplotlib庫(kù)安裝失敗的經(jīng)驗(yàn)總結(jié)(附pycharm配置anaconda)

    最近根據(jù)領(lǐng)導(dǎo)布置的學(xué)習(xí)任務(wù),開(kāi)始學(xué)習(xí)python中的matplotlib,朋友告訴我這個(gè)很簡(jiǎn)單,然而剛踏入安裝的門檻,就遇到了安裝不成功的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于Python中matplotlib庫(kù)安裝失敗的經(jīng)驗(yàn)總結(jié),需要的朋友可以參考下
    2022-08-08
  • 如何基于Python制作有道翻譯小工具

    如何基于Python制作有道翻譯小工具

    這篇文章主要介紹了如何基于Python制作有道翻譯小工具,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 使用Python?http.server模塊共享文件的方法詳解

    使用Python?http.server模塊共享文件的方法詳解

    大家好,今天給大家介紹一下Python標(biāo)準(zhǔn)庫(kù)中的http.server模塊,這個(gè)模塊提供了一種簡(jiǎn)單的方式來(lái)快速啟動(dòng)一個(gè)HTTP服務(wù)器,文中給大家介紹了使用Python?http.server模塊共享文件的方法,需要的朋友可以參考下
    2024-05-05
  • 使用Python代碼實(shí)現(xiàn)Linux中的ls遍歷目錄命令的實(shí)例代碼

    使用Python代碼實(shí)現(xiàn)Linux中的ls遍歷目錄命令的實(shí)例代碼

    這次我就要試著用 Python 來(lái)實(shí)現(xiàn)一下 Linux 中的 ls 命令, 小小地證明下 Python 的不簡(jiǎn)單,需要的朋友可以參考下
    2019-09-09
  • Python實(shí)戰(zhàn)之異步獲取中國(guó)天氣信息

    Python實(shí)戰(zhàn)之異步獲取中國(guó)天氣信息

    這篇文章主要介紹了如何利用Python爬蟲(chóng)異步獲取天氣信息,用的API是中國(guó)天氣網(wǎng)。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手試一試
    2022-03-03
  • Python實(shí)現(xiàn)檢測(cè)文件MD5值的方法示例

    Python實(shí)現(xiàn)檢測(cè)文件MD5值的方法示例

    這篇文章主要介紹了Python實(shí)現(xiàn)檢測(cè)文件MD5值的方法,涉及Python針對(duì)大文件的讀取、判斷、運(yùn)算、加密等相關(guān)操作技巧,需要的朋友可以參考下
    2018-04-04
  • python初學(xué)之用戶登錄的實(shí)現(xiàn)過(guò)程(實(shí)例講解)

    python初學(xué)之用戶登錄的實(shí)現(xiàn)過(guò)程(實(shí)例講解)

    下面小編就為大家分享一篇python初學(xué)之用戶登錄的實(shí)現(xiàn)過(guò)程(實(shí)例講解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • Python Django的安裝配置教程圖文詳解

    Python Django的安裝配置教程圖文詳解

    Django是一個(gè)開(kāi)放源代碼的Web應(yīng)用框架,由Python寫成,Django是重量級(jí)選手中最有代表性的一位。許多成功的網(wǎng)站和APP都基于Django。這篇文章主要介紹了Python Django的安裝配置,需要的朋友可以參考下
    2019-07-07

最新評(píng)論