celery + redis Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused
Asked Answered
B

2

5

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//

Bitty answered 23/2, 2020 at 23:42 Comment(1)
Celery will ignore the parameter broker_url because Celery will look only for parameters that start with CELERY_ and I think that by default Celery try to connect to a rabbitmq server; this is why I get the error Cannot connect to amqp://guest:**@127.0.0.1:5672//Bitty
A
16

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'
Amidships answered 23/9, 2020 at 21:25 Comment(1)
It is still not working.Distinguished
D
1

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.

Dominga answered 14/9, 2021 at 16:33 Comment(1)
This fixed my issue.Adlei

© 2022 - 2024 — McMap. All rights reserved.