【问题标题】:consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 61] Connection refused消费者:无法连接到 amqp://guest:**@127.0.0.1:5672//:[Errno 61] 连接被拒绝
【发布时间】:2020-03-12 09:40:39
【问题描述】:

这是我的项目结构

 myproj
│
├── app1
    ├── __init__.py
    ├── tasks.py

|---gettingstarted
    ├── __init__.py
    ├── urls.py
    ├── settings.py

│   
├── manage.py
|-- Procfile

在入门/设置中:

BROKER_URL = 'redis://'

在 Procfile 中:

web: gunicorn gettingstarted.wsgi --log-file -
worker: celery worker --app=app1.tasks.app

在 app1/tasks.py 中

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

app = celery.Celery('hello')


@app.task
def add(x, y):
   return x + y

当我运行“celery worker”时,它给了我:

 consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 61] Connection refused.

【问题讨论】:

  • Celery 正在尝试连接到默认代理 - rabbitmq,它在 5672 端口上运行。您的代理设置似乎没有被应用。顺便说一句,redis 在端口 6379 上运行。
  • 你能试试把celery.Celery('hello')改成celery.Celery('gettingstarted')吗?

标签: python django redis celery


【解决方案1】:

您没有从 django 设置中配置 celery。致integrate celery with django,最好按照指南操作:

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


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'gettingstarted.settings')
app = celery.Celery('hello')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

@app.task
def add(x, y):
   return x + y

然后在 settings.py 中将 BROKER_URL 更改为 CELERY_BROKER_URL

【讨论】:

    猜你喜欢
    • 2023-01-10
    • 2018-10-24
    • 2020-06-07
    • 2017-11-26
    • 2019-01-10
    • 2018-01-22
    • 2020-12-30
    • 2023-03-14
    相关资源
    最近更新 更多