如何使用 django-celery 配置 TASK_SERIALIZER

发布于 2024-11-19 06:49:28 字数 636 浏览 5 评论 0原文

我正在使用 django-celery,我想将 TASK_SERIALIZER 设置为 JSON 而不是 pickle。

从而在每个方法的基础上执行此操作

@task

我可以通过将我的任务装饰器从 更改为

@task(serializer="json")

但我想在全球范围内执行此操作, 。 settings.py 中的设置

TASK_SERIALIZER="json"

不起作用。尝试运行

import celery
celery.conf.TASK_SERIALIZER="json"

(如此处所暗示的那样)会导致

AttributeError: 'module' object has no attribute 'conf'

任何想法如何配置此设置当通过 django 运行 celery 时?

I'm using django-celery and I'd like to set the TASK_SERIALIZER to JSON instead of pickle.

I can do this on a per-method basis by changing my task decorators from

@task

to

@task(serializer="json")

But I'd like to do it globally. Setting

TASK_SERIALIZER="json"

in settings.py doesn't work. Trying to run

import celery
celery.conf.TASK_SERIALIZER="json"

(as implied here) results in

AttributeError: 'module' object has no attribute 'conf'

Any idea how to configure this setting when running celery through django?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱绻入梦 2024-11-26 06:49:28

想通了。

settings.py 中,您需要设置

CELERY_TASK_SERIALIZER = "json"

文档令人困惑,至少对我而言。

Figured it out.

In settings.py you need to set

CELERY_TASK_SERIALIZER = "json"

Docs are confusing, at least to me.

吻泪 2024-11-26 06:49:28

我发现创建一个 celeryconfig 文件(如文档推荐的那样)使事情变得更加清晰。

celeryconfig.py

# Celery configuration file
BROKER_URL = 'amqp://'
CELERY_RESULT_BACKEND = 'amqp://'

CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'America/Los_Angeles'
CELERY_ENABLE_UTC = True

您可以使用此命令发送设置(一旦您调用 Celery)

celery.config_from_object('celeryconfig')

I found that creating a celeryconfig file (like the docs recommend) makes things a lot cleaner.

celeryconfig.py

# Celery configuration file
BROKER_URL = 'amqp://'
CELERY_RESULT_BACKEND = 'amqp://'

CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'America/Los_Angeles'
CELERY_ENABLE_UTC = True

You can sent set it with this command (once you call Celery)

celery.config_from_object('celeryconfig')
唠甜嗑 2024-11-26 06:49:28

来自 文档

对于任务消息,您可以设置 CELERY_TASK_SERIALIZER 设置
到“json”或“yaml”而不是pickle。目前没有
任务结果的替代解决方案(但编写自定义结果
使用 JSON 的后端是一个简单的任务)

因此设置 CELERY_RESULT_SERIALIZER = "json" 看起来毫无用处。就我而言,结果仍处于泡菜状态(Celery 3.1.3)。是的。我知道...

From the doc :

For the task messages you can set the CELERY_TASK_SERIALIZER setting
to “json” or “yaml” instead of pickle. There is currently no
alternative solution for task results (but writing a custom result
backend using JSON is a simple task)

So setting CELERY_RESULT_SERIALIZER = "json" looks useless. In my case, results are still in pickle (Celery 3.1.3). Yeah. I know...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文