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

Yac*_*izi 2 django redis celery

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
Run Code Online (Sandbox Code Playgroud)

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...
Run Code Online (Sandbox Code Playgroud)

In my settings.py I have this:

BROKER_URL = 'redis://localhost:6379'
Run Code Online (Sandbox Code Playgroud)

requirements.txt:

amqp==2.5.2, asgiref==3.2.3, billiard==3.6.2.0, celery==4.4.0, redis==3.4.1
Run Code Online (Sandbox Code Playgroud)

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))
Run Code Online (Sandbox Code Playgroud)

car_rental/init.py:

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

__all__ = ('celery_app',)
Run Code Online (Sandbox Code Playgroud)

and the structure of my project is like this:

 car_rental
           /car_rental
               __init__.py
               celery.py
               setting.py
Run Code Online (Sandbox Code Playgroud)

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

Dan*_*n91 10

在这种情况下,如果您将参数从 BROKER_URL 更改为 CELERY_BROKER_URL,它应该可以工作。当你在这里给它命名空间时:

app.config_from_object('django.conf:settings', namespace='CELERY')
Run Code Online (Sandbox Code Playgroud)

此时,您需要将 BROKER_URL 参数重命名为 CELERY_BROKER_URL。

CELERY_BROKER_URL = 'redis://localhost:6379'
Run Code Online (Sandbox Code Playgroud)

另一个例子:

app.config_from_object('django.conf:settings', namespace='CAR')
CAR_BROKER_URL = 'redis://localhost:6379'
Run Code Online (Sandbox Code Playgroud)