【问题标题】:Celery: ignoring BROKER_URL in config file芹菜:忽略配置文件中的 BROKER_URL
【发布时间】:2013-11-12 19:58:48
【问题描述】:

我的问题与this one 重复,但更详细。

问题是我在我的 Celery 配置文件中设置了 BROKER_URL,但这并没有反映在我 am 加载配置中:我检查过,它 正在加载 - 事实上,那里定义的其他常量正在被设置,只是不是 BROKER_URL

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


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 被用作序列化程序,而不是 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://guest@127.0.0.1:5672//.

我尝试分手BROKER_URL,但无济于事:

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

有趣的是,当我在app.py 中明确设置BROKER_URL 时,它确实有效:

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"

【问题讨论】:

标签: celery config configuration-files


【解决方案1】:

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

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

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

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

只需删除它:

app = Celery('tasks')

多田!一切正常……我学到了宝贵的一课。

【讨论】:

  • 好吧,我也犯了同样的罪!感谢您回来更新此内容
【解决方案2】:

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

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

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

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

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

【讨论】:

    【解决方案3】:

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

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

    我忘了在我的 celery worker 命令前面加上DJANGO_READ_DOT_ENV_FILE=1

    所以我从:

    celery -A myapp worker -l info

    DJANGO_READ_DOT_ENV_FILE=1 celery -A myapp worker -l info

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

    【讨论】:

      猜你喜欢
      • 2013-10-28
      • 2012-07-29
      • 2016-11-11
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      • 2021-12-08
      • 1970-01-01
      相关资源
      最近更新 更多