婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > python中BackgroundScheduler和BlockingScheduler的區(qū)別

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

熱門標簽:電梯新時達系統(tǒng)外呼顯示e 成都呼叫中心外呼系統(tǒng)哪家強 宿州電話機器人哪家好 無錫智能外呼系統(tǒng)好用嗎 百應電話機器人總部 地圖標注與注銷 西青語音電銷機器人哪家好 旅游廁所地圖標注怎么弄 南昌地圖標注

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

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

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

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

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)蝿照{(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í)行。

到此這篇關(guān)于python中BackgroundScheduler和BlockingScheduler的區(qū)別 的文章就介紹到這了,更多相關(guān)python BackgroundScheduler BlockingScheduler內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python定時任務工具之APScheduler使用方式
  • 詳解Python 定時框架 Apscheduler原理及安裝過程
  • 詳解python調(diào)度框架APScheduler使用
  • Python任務調(diào)度利器之APScheduler詳解
  • Python使用APScheduler實現(xiàn)定時任務過程解析
  • Python APScheduler執(zhí)行使用方法詳解
  • Python任務調(diào)度模塊APScheduler使用
  • Python定時任務APScheduler原理及實例解析
  • Python定時任務APScheduler安裝及使用解析
  • python 基于Apscheduler實現(xiàn)定時任務

標簽:辛集 濰坊 西安 雅安 七臺河 贛州 渭南 許昌

巨人網(wǎng)絡通訊聲明:本文標題《python中BackgroundScheduler和BlockingScheduler的區(qū)別》,本文關(guān)鍵詞  python,中,BackgroundScheduler,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python中BackgroundScheduler和BlockingScheduler的區(qū)別》相關(guān)的同類信息!
  • 本頁收集關(guān)于python中BackgroundScheduler和BlockingScheduler的區(qū)別的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 祁东县| 大宁县| 鄯善县| 察雅县| 广安市| 扶余县| 邵武市| 杨浦区| 新兴县| 调兵山市| 宜丰县| 平江县| 伊宁市| 天等县| 江陵县| 板桥市| 浑源县| 西吉县| 壤塘县| 遵义县| 定边县| 绥阳县| 榆林市| 乌拉特前旗| 海门市| 聂拉木县| 茂名市| 鹿邑县| 临高县| 景东| 扎赉特旗| 鱼台县| 万盛区| 武胜县| 达拉特旗| 当涂县| 保靖县| 武邑县| 扎兰屯市| 沐川县| 綦江县|