使用者:无法连接到amqp:// guest:**@127.0.0.1:5672 //:[Errno 61]连接被拒绝

时间:2019-11-16 12:02:28

标签: python django redis celery

这是我的项目结构

 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

当我运行“芹菜工人”时,它会给我:

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

1 个答案:

答案 0 :(得分:0)

您不是从Django设置配置芹菜。要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