Celery:忽略配置文件中的 BROKER_URL

问题描述 投票:0回答:4

我的问题与这个问题重复,但更详细。

问题是我在 Celery 配置文件中设置了

BROKER_URL
,但这没有反映在我am加载配置中:我检查过,它is正在加载 - 事实上,其他常量已定义,正在设置,只是不是
BROKER_URL

这似乎是一个错误,但我想确定一下。


celeryconfig.py
:

BROKER_URL = "amqp://user:[email protected]:5672//vhost"

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

JSON
被用作序列化器,而不是
Pickle
,所以我知道这是有效的。)

app.py
:

from celery import Celery

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

调用Worker:

celery -A app.app worker -l info

但后来我明白了:

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

我尝试过分手

BROKER_URL
,但没有成功:

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

有趣的是,当我在

BROKER_URL
中明确设置
app.py
时,它确实有效:

from celery import Celery

app = Celery('tasks', broker='amqp://guest@localhost//')
app.config_from_object('celeryconfig')
app.conf.BROKER_URL = "amqp://user:[email protected]:5672//vhost"
celery config configuration-files
4个回答
20
投票

当然,我在完成这个问题后立即意识到我做错了什么,但我仍然发布它,因为有人可能会发现它有用。

我的问题是我复制并粘贴了教程中的代码(*facepalm)。

当我使用

broker
arg 定义应用程序时,我将覆盖配置文件:

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

只需删除它:

app = Celery('tasks')

田田!一切都很好......我学到了宝贵的教训。


8
投票

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

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

您必须将

CELERY_
前缀添加到 celeryconfig.py 中的
BROKER_URL
条目中:

CELERY_BROKER_URL = "amqp://user:[email protected]:5672//vhost"

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

2
投票

对于其他有类似问题的人来说,我也遇到了类似的问题,但原因不同。

我在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

虽然很愚蠢,但这是一个很好的提醒。希望它可以节省一些人的时间。


0
投票

我对此简直要疯了。尽管我按照文档进行操作,但 Celery 忽略了我的 Django 设置。

我跑了

celery -A celery worker -l info

我在 common/celery.py 文件中放置了一个断点(),并意识到它不是正在运行的 celery 应用程序。我必须运行以下命令:

celery -A my_project_name worker -l info
© www.soinside.com 2019 - 2024. All rights reserved.