【发布时间】:2020-06-07 14:43:29
【问题描述】:
我无法运行 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//
【问题讨论】:
-
Celery 将忽略参数
broker_url,因为 Celery 只会查找以CELERY_开头的参数,我认为默认情况下 Celery 会尝试连接到 rabbitmq 服务器;这就是为什么我收到错误Cannot connect to amqp://guest:**@127.0.0.1:5672//