如何使用django-celery配置TASK_SERIALIZER

Leo*_*opd 6 python django celery django-celery

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

我可以通过更改我的任务装饰器来逐个方法地执行此操作

@task
Run Code Online (Sandbox Code Playgroud)

@task(serializer="json")
Run Code Online (Sandbox Code Playgroud)

但我想在全球范围内做到这一点.设置

TASK_SERIALIZER="json"
Run Code Online (Sandbox Code Playgroud)

settings.py不起作用.试着跑

import celery
celery.conf.TASK_SERIALIZER="json"
Run Code Online (Sandbox Code Playgroud)

(为暗示这里)导致

AttributeError: 'module' object has no attribute 'conf'
Run Code Online (Sandbox Code Playgroud)

知道如何通过django运行芹菜时配置此设置?

Leo*_*opd 22

弄清楚了.

settings.py你需要设置

CELERY_TASK_SERIALIZER = "json"
Run Code Online (Sandbox Code Playgroud)

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


Jar*_*der 5

我发现创建一个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
Run Code Online (Sandbox Code Playgroud)

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

celery.config_from_object('celeryconfig')
Run Code Online (Sandbox Code Playgroud)