I am trying to use celery 3.1.17 with a jar, but I am stuck with the "No Result" error. I'm new to flask and celery, so I guess I'm doing something wrong. If I use this code, everything goes well, but when I parse the code into different files, I get this error. I missed something, but what? I use Flask-Script to run the application using "manage.py runningerver", file structure:
.
├── app
│ ├── core.py
│ ├── extensions.py
│ └── __init__.py
├── manage.py
└── settings.py
With settings.py - only settings
DEBUG = True
SECRET_KEY = 'not_a_secret'
CELERY_BROKER_URL = 'amqp://'
CELERY_RESULT_BACKEND = 'amqp'
manage.py - launches application n
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: creates the application and make_celery as docs say
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: starts celery and defines a simple task
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 defines two routes: one to run the task and one to get the result
# -*- 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, /test/result/i, :
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.
?
/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])
. , .
.