2015-09-06 103 views
5

我想熟悉燒瓶APScheduler插件通過下面的示例代碼:https://github.com/viniciuschiele/flask-apscheduler/blob/master/examples/jobs.py#L1如何使用燒瓶APScheduler中存在的瓶應用

我的項目具有以下結構:

backend 
    run.py 
    application 
     __init__.py 
     utilities 
      __init__.py 
      views 
      models 

其中,

backend>run.py是:

from application import app 
app.run(debug=True) 
from application import scheduler 
scheduler.start() 

backend>application>__init__.py是:

from flask import Flask 
app = Flask(__name__) 
from application.utilities.views import Config 
from flask_apscheduler import APScheduler 
app.config.from_object(Config()) 
scheduler = APScheduler() 
scheduler.init_app(app) 

backend>application>utilities>__init__.py是空

backend>application>utilities>models.py是空

backend>application>utilities>views.py是:

class Config(object): 
    JOBS = [ 
     { 
      'id': 'job1', 
      'func': 'application:utilities:views:job1', 
      'args': (1, 2), 
      'trigger': { 
       'type': 'cron', 
       'second': 10 
      } 
     } 
    ] 

def job1(a, b): 
    print(str(a) + ' ' + str(b)) 

不過,我得到以下錯誤:

(env)$ python run.py local 
Traceback (most recent call last): 
    File "run.py", line 1, in <module> 
    from application import app 
    File "HOME/backend/application/__init__.py", line 106, in <module> 
    scheduler.init_app(app) 
    File "/home/xxxxxx/.anaconda/envs/env/lib/python2.7/site-packages/flask_apscheduler/scheduler.py", line 73, in init_app 
    self.__load_jobs(app) 
    File "/home/xxxxxx/.anaconda/envs/env/lib/python2.7/site-packages/flask_apscheduler/scheduler.py", line 136, in __load_jobs 
    self.__load_job(job, app) 
    File "/home/xxxxxx/.anaconda/envs/env/lib/python2.7/site-packages/flask_apscheduler/scheduler.py", line 159, in __load_job 
    func = ref_to_obj(func) 
    File "/home/xxxxxx/.anaconda/envs/env/lib/python2.7/site-packages/apscheduler/util.py", line 264, in ref_to_obj 
    raise LookupError('Error resolving reference %s: error looking up object' % ref) 
LookupError: Error resolving reference application:utilities:views:job1: error looking up object 

我的結構看起來好嗎?把正確的代碼放在正確的地方嗎?我應該改變什麼才能使它工作?

回答

7

您的參考應該只有一個冒號(「:」)。冒號將必需的導入與必須查找的變量分開。所以:

'func': 'application.utilities.views:job1'