首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django,芹菜和kombu:如何允许泡菜?

Django,芹菜和kombu:如何允许泡菜?
EN

Stack Overflow用户
提问于 2021-07-14 13:27:31
回答 1查看 222关注 0票数 1

我想使用picker作为芹菜序列化程序。我收到了一个kombu错误,不允许使用泡菜。

代码语言:javascript
复制
kombu.exceptions.ContentDisallowed: Refusing to deserialize untrusted content of type pickle (application/x-python-serialize)

当我尝试用.delay提交一个任务时发生异常,这个任务有一个datetime参数。

芹菜文档说(https://docs.celeryproject.org/projects/kombu/en/master/userguide/serialization.html#guide-serialization)

代码语言:javascript
复制
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:

但我不知道如何实现它。

我有过

celeryredis==5.1.2

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

代码语言:javascript
复制
CELERY_TASK_SERIALIZER = 'pickle'

这会导致错误。

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

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

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2021-07-14 14:44:52

因此,这似乎是可行的:

在settings.py中

代码语言:javascript
复制
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中运行。

我这样做是因为datetime参数不知何故不起作用而让它很恼火。我不明白为什么解决方案如此复杂。Django有一个处理datetime的json序列化器。为什么芹菜不能用呢?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68372501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档