14

My question is a duplicate of this one, but more detailed.

The problem is that I have a BROKER_URL set in my Celery config file, but that isn't reflected in and I am loading the config: I checked, and it is being loaded - in fact, other constants defined there are being set, just not BROKER_URL.

This appears to be a bug, but I wanted to be sure.


celeryconfig.py:

BROKER_URL = "amqp://user:password@remote.server.com:5672//vhost"

CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ENABLE_UTC = True

(JSON is being used as the serializer, not Pickle, so I know this is working.)

app.py:

from celery import Celery

app = Celery('tasks', broker='amqp://guest@localhost//')
app.config_from_object('celeryconfig')

Invoking the Worker:

celery -A app.app worker -l info

But then I get this:

[2013-11-12 11:20:51,610: INFO/MainProcess] consumer: Connected to amqp://guest@127.0.0.1:5672//.

I tried breaking up BROKER_URL, but to no avail:

BROKER_TRANSPORT = 'amqp'
BROKER_USER = 'user'
BROKER_PASSWORD = 'password'
BROKER_HOST = 'remote.server.com'
BROKER_PORT = 5672
BROKER_VHOST = '/vhost'

Interestingly, it does work when I explicitly set the BROKER_URL in app.py:

from celery import Celery

app = Celery('tasks', broker='amqp://guest@localhost//')
app.config_from_object('celeryconfig')
app.conf.BROKER_URL = "amqp://user:password@remote.server.com:5672//vhost"
4

3 回答 3

18

Of course, I realized what I'd done wrong immediately after finishing this question, but I still posted it because someone might find it useful.

My problem is that I copied and pasted code from the tutorial (*facepalm).

I'm overriding the config file when I define the app with a broker arg:

app = Celery('tasks', broker='amqp://guest@localhost//')

Simply remove that:

app = Celery('tasks')

Tada! Everything works fine... and I learned a valuable lesson.

于 2013-11-12T19:58:48.350 回答
7

只是为了澄清,因为你正在使用这个:

app.config_from_object('django.conf:settings', namespace='CELERY')

您必须在 celeryconfig.pyCELERY_中的条目中添加前缀:BROKER_URL

CELERY_BROKER_URL = "amqp://user:password@remote.server.com:5672//vhost"

CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ENABLE_UTC = True
于 2017-04-09T20:39:09.280 回答
1

只是对于其他有类似问题的人,我遇到了一个不同原因的类似问题。

我在 heroku 上使用 celery 和 django,但在我的开发环境中,我使用django-environ从文件中填充环境变量。

我忘了在我的芹菜工人命令前加上DJANGO_READ_DOT_ENV_FILE=1

所以我从:

celery -A myapp worker -l info

DJANGO_READ_DOT_ENV_FILE=1 celery -A myapp worker -l info

愚蠢,但很好的提醒。希望它可以节省一些时间。

于 2018-12-16T22:05:56.347 回答