Celery + rabbitmq in result backend

15,432

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!

Share:
15,432

Related videos on Youtube

Leon
Author by

Leon

Updated on June 04, 2022

Comments

  • Leon
    Leon almost 2 years

    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!

  • chachan
    chachan over 8 years
    I'm looking for how to use Rabbit as both broker and backend but I found in the docs that it shouldn't use amqp. docs
  • Amichai Schreiber
    Amichai Schreiber over 7 years
    @chachan that link is broken - can you elaborate?
  • chachan
    chachan over 7 years
    @AmichaiSchreiber you're right. I don't see it anymore. Sorry