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 "", line 11, in 
  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

转载于:https://www.cnblogs.com/ljbguanli/p/7218026.html

你可能感兴趣的:(APScheduler: LookupError: No trigger by the name "interval" was found)