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

为君一笑 提交于 2019-12-24 20:02:40

问题


This is my project structure

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

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

│   
├── manage.py
|-- Procfile

In gettingstarted/settings:

BROKER_URL = 'redis://'

In Procfile:

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

In 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

When I run "celery worker" it gives me:

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

回答1:


You're not configuring celery from your django settings. To integrate celery with django, it's best to just follow the guide:

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

And in settings.py change BROKER_URL to CELERY_BROKER_URL.



来源:https://stackoverflow.com/questions/58890516/consumer-cannot-connect-to-amqp-guest127-0-0-15672-errno-61-connect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!