5

tasks.py:

from celery import Celery

app = Celery('tasks', broker='amqp://guest@localhost//')

@app.task
def add(x, y):
    return x + y

call.py:

from tasks import add

result = add.delay(1, 4)

result.ready()

celeryconfig.py: (in the same directory)

BROKER_URL = 'amqp://guest@localhost//'

CELERY_RESULT_BACKEND = 'amqp://guest@localhost//'

CELERY_TASK_SERIALIZER = 'json'

CELERY_RESULT_SERIALIZER = 'json'

CELERY_ACCEPT_CONTENT=['json']

CELERY_TIMEZONE = 'Europe/Oslo'

CELERY_ENABLE_UTC = True

In call.py I have en error:

AttributeError: 'DisabledBackend' object has no attribute '_get_task_meta_for'

I read the docs, I have result backend, why it does not work? How to fix that?

Thanks!

Leon
  • 6,316
  • 19
  • 62
  • 97

2 Answers2

9

You should not use Celery('tasks', broker='amqp://guest@localhost//') along with your celeryconfig.py file.

The way you should go (and the way of the documentation) is:

Create a package called mypackage (it is assumed that mypackage is not a sub package) with two files:

celery.py

from __future__ import absolute_import

from celery import Celery

app = Celery('mypackage',
             broker='amqp://guest@localhost//',
             backend='amqp://guest@localhost//',
             include=['mypackage.tasks'] #References your tasks. Donc forget to put the whole absolute path.
             )

app.conf.update(
        CELERY_TASK_SERIALIZER = 'json',
        CELERY_RESULT_SERIALIZER = 'json',
        CELERY_ACCEPT_CONTENT=['json'],
        CELERY_TIMEZONE = 'Europe/Oslo',
        CELERY_ENABLE_UTC = True
                )

tasks.py

from mypackage.celery import app
@app.task
def add(x, y):
    return x + y

Your call.py file is fine.

Start the Celery worker by using celery -A mypackage worker in command line, in the parent directory of mypackage.

Then you can start another Python interpreter, use call.py and voila!

Flavian Hautbois
  • 2,940
  • 6
  • 28
  • 45
-4

I find a bug in my code. In tasks.py:

app = Celery('tasks', broker='amqp://guest@localhost//', backend='amqp')

Needs backend parameter.

Leon
  • 6,316
  • 19
  • 62
  • 97
  • 1
    If you do that you will have issues with your configuration file, as you have a backend and a broker set twice. Assume you want to use a remote RabbitMQ broker, then which one do you change? – Flavian Hautbois Oct 13 '14 at 07:48