Django 、 celery 和昆布 : how to allow pickle?

标签 django django-celery kombu

我想使用picker作为celery序列化器。 我收到一条昆布错误,指出不允许使用 pickle。

kombu.exceptions.ContentDisallowed: Refusing to deserialize untrusted content of type pickle (application/x-python-serialize)

当我尝试使用.delay提交任务时发生异常 此任务有一个日期时间参数。

Celery 文档显示 ( https://docs.celeryproject.org/projects/kombu/en/master/userguide/serialization.html#guide-serialization )

By default Kombu will only load JSON messages, so if you want to use other serialization format you must explicitly enable them in your consumer by using the accept argument:

但我不知道如何实现。

我有

celery [redis]==5.1.2

在我的项目的settings.py中我已经尝试过

CELERY_TASK_SERIALIZER = 'pickle'

这会导致错误。

(这里没有记录此设置,它是旧的吗?https://docs.celeryproject.org/en/stable/userguide/configuration.html#task-settings)

这是celery.py的内容 我试图理解Celery not accepting pickle even after allowing it

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from kombu import serialization

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

app = Celery('project')

# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.

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

accept_content = ['pickle', 'application/x-python-serialize']
task_serializer = 'pickle'
result_serializer = 'pickle'
serialization.register_pickle()
serialization.enable_insecure_serializers()

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

最佳答案

看来这可行:

在设置.py

CELERY_BROKER_URL = 'redis://redis:6379/0'
CELERY_RESULT_BACKEND = 'redis://redis:6379/0'
CELERY_TASK_SERIALIZER = 'pickle'   # changed, was json
CELERY_ACCEPT_CONTENT = ['json', 'pickle']  # new
os.environ.setdefault('C_FORCE_ROOT', 'true')  # new

有很多关于我正在做的事情的邪恶警告。 这是在单租户机器上的 docker 中运行的。

我这样做是因为它对日期时间参数无法正常工作感到恼火。我不明白为什么解决方案如此复杂。 Django 有一个处理日期时间的 json 序列化器。为什么 celery 不能用它?

关于 Django 、 celery 和昆布 : how to allow pickle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68372501/

相关文章:

Celery AsyncResult 获取主机名

python - 任务状态和 django-celery

python - 使用Kombu ConsumerMixin,如何声明多个绑定(bind)?

python - 如何使用 Kombu 指定 AMQP correlationId?

django - 在 Django 表单中存储动态字段

python - 如何更改表单中显示的字段名称

sql - Django 复杂的过滤器和顺序

django - Django:在模板中显示当前语言环境

python - django celery 击败 DBAccessError

python - 昆布+RabbitMQ : Check whether a queue is empty