gpt4 book ai didi

flask - 如何在现有 Flask 应用中使用 Flask-APScheduler

转载 作者:行者123 更新时间:2023-12-01 19:40:28 34 4
gpt4 key购买 nike

我尝试通过以下示例代码来熟悉Flask-APScheduler插件:https://github.com/viniciuschiele/flask-apscheduler/blob/master/examples/jobs.py#L1

我的项目具有以下结构:

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

后端>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)

后端>应用程序>实用程序>__init__.py为空

backend>application>utilities>models.py 为空

后端>应用程序>实用程序>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

我的结构看起来还好吗?是否已将正确的代码放置在正确的位置?我应该更改什么才能使其正常工作?

最佳答案

您的引用文献只能有一个冒号(“:”)。冒号将所需的导入与必须查找的变量分隔开。所以:

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

关于flask - 如何在现有 Flask 应用中使用 Flask-APScheduler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32424148/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com