我正在尝试使用带有烧瓶的芹菜3.1.17,但我遇到了“无结果后端配置”错误。 我是烧瓶和芹菜的新手,所以我可能做错了。 如果我使用this代码一切顺利,但是当我将代码分成不同的文件时,我得到了这个错误。我错过了什么,但是什么? 我正在使用Flask-Script运行带有“manage.py runserver”的应用程序,文件结构为:
.
├── app
│ ├── core.py
│ ├── extensions.py
│ └── __init__.py
├── manage.py
└── settings.py
使用settings.py - 只需设置
# -*- coding: utf-8 -*-
DEBUG = True
SECRET_KEY = 'not_a_secret'
CELERY_BROKER_URL = 'amqp://'
CELERY_RESULT_BACKEND = 'amqp'
manage.py - 运行applicationn
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os
from app import create_app
from flask.ext.script import Manager, Shell, Server
app = create_app()
manager = Manager(app)
manager.add_command("runserver", Server(host="0.0.0.0", port=5032))
def make_shell_context():
return dict(app=app)
manager.add_command('shell', Shell(make_context=make_shell_context))
if __name__ == '__main__':
manager.run()
app / init .py:创建app和make_celery为docs说
from __future__ import absolute_import
from flask import Flask
from .extensions import celery, make_celery
import settings
def create_app():
app = Flask(__name__)
app.config.from_object(settings)
make_celery(app, celery)
from .core import core as core_blueprint
app.register_blueprint(core_blueprint)
return app
app / extensions.py:实例化celery并定义简单任务
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from celery import Celery
def make_celery(app, celery):
celery = Celery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'], broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
print 'passo da make_celery'
return celery
celery = Celery()
@celery.task(name="tasks.add")
def add(x, y):
return x + y
app / core.py定义了两个路由:一个用于运行任务,另一个用于获取结果
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from flask import Flask, Blueprint, abort, jsonify, request, session
from app.extensions import add
core = Blueprint('core', __name__)
@core.route("/test")
def hello_world(x=16, y=16):
x = int(request.args.get("x", x))
y = int(request.args.get("y", y))
res = add.apply_async((x, y))
context = {"id": res.task_id, "x": x, "y": y}
result = "add((x){}, (y){})".format(context['x'], context['y'])
goto = "{}".format(context['id'])
return jsonify(result=result, goto=goto)
@core.route("/test/result/<task_id>")
def show_result(task_id):
retval = add.AsyncResult(task_id).get(timeout=1.0)
return repr(retval)
启动celery -A app.extensions.celery worker -l debug
后我可以去/测试但是如果我尝试去/ test / result /我会收到以下错误:
File "/virt/biscelery/lib/python2.7/site-packages/celery/result.py", line 169, in get
no_ack=no_ack,
File "/virt/biscelery/lib/python2.7/site-packages/celery/backends/base.py", line 597, in _is_disabled
'No result backend configured. '
NotImplementedError: No result backend configured. Please see the documentation for more information.
为什么芹菜松散后端配置? 如果我在celery / backends / init .py中修改了
中的get_backend_cls函数def get_backend_cls(backend=None, loader=None):
"""Get backend class by name/alias"""
backend = backend or 'disabled'
loader = loader or current_app.loader
aliases = dict(BACKEND_ALIASES, **loader.override_backends)
try:
return symbol_by_name(backend, aliases)
except ValueError as exc:
reraise(ValueError, ValueError(UNKNOWN_BACKEND.format(
backend, exc)), sys.exc_info()[2])
到
def get_backend_cls(backend=None, loader=None):
"""Get backend class by name/alias"""
backend = backend or 'amqp'
loader = loader or current_app.loader
aliases = dict(BACKEND_ALIASES, **loader.override_backends)
try:
return symbol_by_name(backend, aliases)
except ValueError as exc:
reraise(ValueError, ValueError(UNKNOWN_BACKEND.format(
backend, exc)), sys.exc_info()[2])
一切都开始了。我无法弄清楚我做错了什么。 任何帮助真的很感激。