APScheduler: LookupError: No trigger by the name "interval" was found


APScheduler: LookupError: No trigger by the name “interval” was found

環境

python: 2.6.6
PyInstaller: 2.1
APScheduler: 開始是3.0.1,后來是3.0.5

問題一

問題描寫敘述

曾經在別的機器上開發的python程序(python2.7),在新的機器上執行時報錯

LookupError: No trigger by the name "interval" was found

程序代碼

import os, time
from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler

def myjob():
    print('myjob: %s' % datetime.now())
    time.sleep(5)

if __name__ == '__main__':
    scheduler = BackgroundScheduler()
    scheduler.add_job(myjob, 'interval', seconds=1)
    scheduler.start()

    try:
        while True:
            time.sleep(5)
    except (KeyboardInterrupt, SystemExit):
        scheduler.shutdown()

原因

是因為低版本號的setuptools導致

解決的方法

sudo pip install --upgrade setuptools
sudo pip install --ignore-installed apscheduler

然后再次執行上面的python代碼,問題解決。

問題二

問題描寫敘述

第一個問題解決后,在執行使用pyinstaller打包生成的可執行文件的時候報錯

Traceback (most recent call last):
  File "<string>", line 11, in <module>
  File ".../out00-PYZ.pyz/apscheduler.schedulers.base", line 330, in add_job
  File ".../out00-PYZ.pyz/apscheduler.schedulers.base", line 782, in _create_trigger
  File ".../out00-PYZ.pyz/apscheduler.schedulers.base", line 766, in _create_plugin_instance
LookupError: No trigger by the name "interval" was found

原因

感覺好像是因為pyinstaller打包的時候使用了錯誤版本號的APScheduler。(不確定)???

解決的方法

不要在add_job方法中使用“’interval’, seconds=1”做trigger。而是先創建一個IntervalTrigger對象。然后add_job的時候使用這個對象,即:

改動原來代碼中

    scheduler.add_job(myjob, 'interval', seconds=1)

 trigger = IntervalTrigger(seconds=1) scheduler.add_job(myjob, trigger)

完整代碼例如以下

def myjob():
    print('myjob: %s' % datetime.now())
    time.sleep(5)

if __name__ == '__main__':
    scheduler = BackgroundScheduler()
    trigger = IntervalTrigger(seconds=1)
    scheduler.add_job(myjob, trigger)
    scheduler.start()

    try:
        while True:
            time.sleep(5)
    except (KeyboardInterrupt, SystemExit):
        scheduler.shutdown()

然后用PyInstaller又一次打包,此時再執行可執行文件的時候就不會報錯了。

轉載請以鏈接形式標明本文地址
本文地址:http://blog.csdn.net/kongxx/article/details/50501605


免責聲明!

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



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