这是我的项目结构
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.
答案 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
。