celery + redis 无法连接到 amqp://guest:**@127.0.0.1:5672//: [Errno 111] 连接被拒绝

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

我不能运行 celery worker + redis + django。如果我 运行 此命令检查 celery worker 是否准备好接收任务:

celery -A car_rental worker -l info

我收到这个错误:

[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...

在我的 settings.py 我有这个:

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',)

我的项目结构是这样的:

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

我不明白的是我在broker_url = 'redis://localhost:6379'中使用但是在错误中我有:Cannot connect to amqp://guest:**@127.0.0.1:5672//

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

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

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

CELERY_BROKER_URL = 'redis://localhost:6379'

另一个例子:

app.config_from_object('django.conf:settings', namespace='CAR')
CAR_BROKER_URL = 'redis://localhost:6379'

我已经完全按照 Dantheman91 的解释配置了芹菜,但我仍然遇到了同样的问题。

我的项目中没有使用 settings.py,而是在名为设置。

因此,如果有人在这些 roles/instances 之间切换,请不要忘记在 celery.py 中进行相同的更改。

在类似的情况下,使用:

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

它应该与您在项目 manage.py 中使用的相同。