问题
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