如何用Django芹菜配置TASK_SERIALIZER

2024-05-19 02:52:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我可以在每个方法的基础上通过从

@task

@task(serializer="json")

但我想在全球范围内做。设置

TASK_SERIALIZER="json"

insettings.py不起作用。试图逃跑

import celery
celery.conf.TASK_SERIALIZER="json"

(正如暗示的here)导致

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

在django中运行芹菜时,知道如何配置此设置吗?


Tags: django方法jsontaskconf基础pickle全球
3条回答

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)

所以设置CELERY_RESULT_SERIALIZER = "json"看起来没用。在我的例子中,结果仍然是腌菜(芹菜3.1.3)。是 啊。我知道。。。

我发现创建一个celleryconfig文件(就像docs推荐的那样)会让事情变得更干净。

芹菜配置.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

你可以用这个命令发送set(一旦你调用芹菜)

celery.config_from_object('celeryconfig')

明白了。

settings.py中,您需要设置

CELERY_TASK_SERIALIZER = "json"

医生们很困惑,至少对我来说。

相关问题 更多 >

    热门问题