skip to Main Content

I can not run the celery worker + redis + django. If I run this command to check that celery worker is ready to receive tasks:

celery -A car_rental worker -l info

I got this error:

[2020-02-24 00:14:42,188: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
Trying again in 2.00 seconds...

In my settings.py I have this:

BROKER_URL = 'redis://localhost:6379'

requirements.txt:

amqp==2.5.2, asgiref==3.2.3, billiard==3.6.2.0, celery==4.4.0, redis==3.4.1

celery.py:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'car_rental.settings')

app = Celery('car_rental')

app.config_from_object('django.conf:settings', namespace='CELERY')

app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

car_rental/init.py:

from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app

__all__ = ('celery_app',)

and the structure of my project is like this:

 car_rental
           /car_rental
               __init__.py
               celery.py
               setting.py

What I didn’t understand is that I am using in the broker_url = 'redis://localhost:6379' but in the error I have: Cannot connect to amqp://guest:**@127.0.0.1:5672//

2

Answers


  1. In this case it should work if you change the parameter from BROKER_URL to CELERY_BROKER_URL. When you gave it the namespace here:

    app.config_from_object('django.conf:settings', namespace='CELERY')
    

    At that point you’ll want to rename your BROKER_URL parameter to CELERY_BROKER_URL.

    CELERY_BROKER_URL = 'redis://localhost:6379'
    

    Another example:

    app.config_from_object('django.conf:settings', namespace='CAR')
    CAR_BROKER_URL = 'redis://localhost:6379'
    
    Login or Signup to reply.
  2. I had configured the celery exactly as Dantheman91 explained, and still, I faced the same issue.

    Instead of having one settings.py in my project, I was using, base.py, development.py, production.py, and test.py inside the module named settings.

    So, in case anyone switching between these roles/instances do not forget to change the same in celery.py.

    In similar case scenario, use:

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj_name.settings.development')
    

    It should be the same as you use in the manage.py of your project.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search