APScheduler中兩種調度器的區別及使用過程中要注意的問題


摘要

本文介紹APScheduler最基本的用法“定時幾秒后啟動job”,解釋其中兩種調度器BackgroundScheduler和BlockingScheduler的區別,說明了如何做到“讓job在start()后就開始運行”,詳述“job執行時間大於定時調度時間”這種特殊情況的問題及解決方法,並說明了每個job都會以thread的方式被調度。

 

基本的定時調度

APScheduler是python的一個定時任務調度框架,能實現類似linux下crontab類型的任務,使用起來比較方便。它提供基於固定時間間隔、日期以及crontab配置類似的任務調度,並可以持久化任務,或將任務以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()

1

2

3

4

5

6

7

8

9

10

11

12

它能實現每隔3s就調度job()運行一次,所以程序每隔3s就輸出’job 3s’。通過修改add_job()的參數seconds,就可以改變任務調度的間隔時間。

 

BlockingScheduler與BackgroundScheduler區別

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

 

BlockingScheduler: 調用start函數后會阻塞當前線程。當調度器是你應用中唯一要運行的東西時(如上例)使用。

BackgroundScheduler: 調用start后主線程不會阻塞。當你不運行任何其他框架時使用,並希望調度器在你應用的后台執行。

下面用兩個例子來更直觀的說明兩者的區別。

 

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

        print('main 1s')

        time.sleep(1)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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

 

job 3s

job 3s

job 3s

job 3s

1

2

3

4

可見,BlockingScheduler調用start函數后會阻塞當前線程,導致主程序中while循環不會被執行到。

 

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)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

可見,BackgroundScheduler調用start函數后並不會阻塞當前線程,所以可以繼續執行主程序中while循環的邏輯。

 

main 1s

main 1s

main 1s

job 3s

main 1s

main 1s

main 1s

job 3s

1

2

3

4

5

6

7

8

通過這個輸出,我們也可以發現,調用start函數后,job()並不會立即開始執行。而是等待3s后,才會被調度執行。

 

如何讓job在start()后就開始運行

如何才能讓調度器調用start函數后,job()就立即開始執行呢?

 

其實APScheduler並沒有提供很好的方法來解決這個問題,但有一種最簡單的方式,就是在調度器start之前,就運行一次job(),如下

 

from apscheduler.schedulers.background import BackgroundScheduler

import time

 

def job():

    print('job 3s')

 

 

if __name__=='__main__':

    job()

    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)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

這樣就能得到如下的輸出

 

job 3s

main 1s

main 1s

main 1s

job 3s

main 1s

main 1s

main 1s

1

2

3

4

5

6

7

8

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

 

如果job執行時間過長會怎么樣

如果執行job()的時間需要5s,但調度器配置為每隔3s就調用一下job(),會發生什么情況呢?我們寫了如下例子:

 

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)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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

 

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

1

2

3

4

5

6

7

8

9

10

11

12

13

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

 

為了能讓多個job()同時運行,我們也可以配置調度器的參數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)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

 

main 1s

main 1s

main 1s

job 3s

main 1s

main 1s

main 1s

job 3s

main 1s

main 1s

main 1s

job 3s

1

2

3

4

5

6

7

8

9

10

11

12

每個job是怎么被調度的

通過上面的例子,我們發現,調度器是定時調度job()函數,來實現調度的。

 

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

 

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

 

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)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

 

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

 

參考

https://www.cnblogs.com/quijote/p/4385774.html

http://debugo.com/apscheduler/

https://stackoverflow.com/questions/34020161/python-apscheduler-skipped-maximum-number-of-running-instances-reached

--------------------- 

作者:ybdesire 

來源:CSDN 

原文:https://blog.csdn.net/ybdesire/article/details/82228840 

版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

摘要本文介紹APScheduler最基本的用法“定時幾秒后啟動job”,解釋其中兩種調度器BackgroundScheduler和BlockingScheduler的區別,說明了如何做到“讓job在start()后就開始運行”,詳述“job執行時間大於定時調度時間”這種特殊情況的問題及解決方法,並說明了每個job都會以thread的方式被調度。
基本的定時調度APScheduler是python的一個定時任務調度框架,能實現類似linux下crontab類型的任務,使用起來比較方便。它提供基於固定時間間隔、日期以及crontab配置類似的任務調度,並可以持久化任務,或將任務以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()123456789101112它能實現每隔3s就調度job()運行一次,所以程序每隔3s就輸出’job 3s’。通過修改add_job()的參數seconds,就可以改變任務調度的間隔時間。
BlockingScheduler與BackgroundScheduler區別APScheduler中有很多種不同類型的調度器,BlockingScheduler與BackgroundScheduler是其中最常用的兩種調度器。那他們之間有什么區別呢? 簡單來說,區別主要在於BlockingScheduler會阻塞主線程的運行,而BackgroundScheduler不會阻塞。所以,我們在不同的情況下,選擇不同的調度器:
BlockingScheduler: 調用start函數后會阻塞當前線程。當調度器是你應用中唯一要運行的東西時(如上例)使用。BackgroundScheduler: 調用start后主線程不會阻塞。當你不運行任何其他框架時使用,並希望調度器在你應用的后台執行。下面用兩個例子來更直觀的說明兩者的區別。
BlockingScheduler的真實例子from apscheduler.schedulers.blocking import BlockingSchedulerimport 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):        print('main 1s')        time.sleep(1)1234567891011121314151617運行這個程序,我們得到如下的輸出:
job 3sjob 3sjob 3sjob 3s1234可見,BlockingScheduler調用start函數后會阻塞當前線程,導致主程序中while循環不會被執行到。
BackgroundScheduler的真實例子from apscheduler.schedulers.background import BackgroundSchedulerimport 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)1234567891011121314151617可見,BackgroundScheduler調用start函數后並不會阻塞當前線程,所以可以繼續執行主程序中while循環的邏輯。
main 1smain 1smain 1sjob 3smain 1smain 1smain 1sjob 3s12345678通過這個輸出,我們也可以發現,調用start函數后,job()並不會立即開始執行。而是等待3s后,才會被調度執行。
如何讓job在start()后就開始運行如何才能讓調度器調用start函數后,job()就立即開始執行呢?
其實APScheduler並沒有提供很好的方法來解決這個問題,但有一種最簡單的方式,就是在調度器start之前,就運行一次job(),如下
from apscheduler.schedulers.background import BackgroundSchedulerimport time
def job():    print('job 3s')

if __name__=='__main__':    job()    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)12345678910111213141516這樣就能得到如下的輸出
job 3smain 1smain 1smain 1sjob 3smain 1smain 1smain 1s12345678這樣雖然沒有絕對做到“讓job在start()后就開始運行”,但也能做到“不等待調度,而是剛開始就運行job”。
如果job執行時間過長會怎么樣如果執行job()的時間需要5s,但調度器配置為每隔3s就調用一下job(),會發生什么情況呢?我們寫了如下例子:
from apscheduler.schedulers.background import BackgroundSchedulerimport 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)1234567891011121314151617運行這個程序,我們得到如下的輸出:
main 1smain 1smain 1sjob 3smain 1smain 1smain 1sExecution 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 1smain 1smain 1sjob 3smain 1s12345678910111213可見,3s時間到達后,並不會“重新啟動一個job線程”,而是會跳過該次調度,等到下一個周期(再等待3s),又重新調度job()。
為了能讓多個job()同時運行,我們也可以配置調度器的參數max_instances,如下例,我們允許2個job()同時運行:
from apscheduler.schedulers.background import BackgroundSchedulerimport 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)12345678910111213141516運行程序,我們得到如下的輸出:
main 1smain 1smain 1sjob 3smain 1smain 1smain 1sjob 3smain 1smain 1smain 1sjob 3s123456789101112每個job是怎么被調度的通過上面的例子,我們發現,調度器是定時調度job()函數,來實現調度的。
那job()函數會被以進程的方式調度運行,還是以線程來運行呢?
為了弄清這個問題,我們寫了如下程序:
from apscheduler.schedulers.background import BackgroundSchedulerimport 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)12345678910111213141516運行程序,我們得到如下的輸出:
main 1smain 1smain 1sjob thread_id-10644, process_id-8872main 1smain 1smain 1sjob thread_id-3024, process_id-8872main 1smain 1smain 1sjob thread_id-6728, process_id-8872main 1smain 1smain 1sjob thread_id-11716, process_id-887212345678910111213141516可見,每個job()的進程ID都相同,但線程ID不同。所以,job()最終是以線程的方式被調度執行。
參考https://www.cnblogs.com/quijote/p/4385774.htmlhttp://debugo.com/apscheduler/https://stackoverflow.com/questions/34020161/python-apscheduler-skipped-maximum-number-of-running-instances-reached--------------------- 作者:ybdesire 來源:CSDN 原文:https://blog.csdn.net/ybdesire/article/details/82228840 版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM