python - celery 使用 'application/x-python-serialize' 而不是 `application/json`

标签 python python-2.7 celery

我正在使用 celery 模块 v。 3.1.25 在 Python 2.7 和 Windows 10 中运行 Celery worker。结果必须以 json 而不是 pickle 编码返回。

问题:当 worker 返回任务结果时,RabbitMQ 管理控制台显示结果为 content_type: application/x-python-serialize。当我们将task_serializerresult_serializeraccept_content设置为时,为什么它仍然是x-python-serialize json?

proj/celery.py

from __future__ import absolute_import, unicode_literals
from celery import Celery

app = Celery('tasks', 
    broker='amqp://test:test@192.168.1.26:5672//',    # running in Win10 VM
    backend='amqp://',
    task_serializer='json',
    result_serializer='json',
    accept_content=['application/json'],
    include=['proj.tasks'])

proj/tasks.py

from __future__ import absolute_import, unicode_literals
from .celery import app

@app.task
def myTask():
    ...
    return ...

worker 开始使用

celery -A proj worker --loglevel=info

并给出关于 pickle 序列化程序的警告

Starting from version 3.2 Celery will refuse to accept pickle by default.

The pickle serializer is a security concern as it may give attackers
the ability to execute any command.  It's important to secure
your broker from unauthorized access when using pickle, so we think
that enabling pickle should require a deliberate action and not be
the default choice.

If you depend on pickle then you should set a setting to disable this
warning and to be sure that everything will continue working
when you upgrade to Celery 3.2::

    CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']

You must only enable the serializers that you will actually use.


  warnings.warn(CDeprecationWarning(W_PICKLE_DEPRECATED))

 -------------- celery@Y-PC v3.1.25 (Cipater)
---- **** -----
--- * ***  * -- Windows-10-10.0.14393
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app:         tasks:0x40ffeb8
- ** ---------- .> transport:   amqp://test:**@192.168.1.26:5672//
- ** ---------- .> results:     amqp://
- *** --- * --- .> concurrency: 12 (prefork)
-- ******* ----
--- ***** ----- [queues]
 -------------- .> celery           exchange=celery(direct) key=celery

最佳答案

将 Celery() 配置参数更改为 accept_content=['json'], 而不是 application/json 是否有帮助?

关于python - celery 使用 'application/x-python-serialize' 而不是 `application/json`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43743286/

相关文章:

如果类实例化对象不存在,Python 避免方法调用?

python-2.7 - 在 Python 2 中使用逗号分隔符格式化数字并四舍五入到小数点后两位?

django - 直接调用绑定(bind)任务时的 Mocking Celery `self.request` 属性

python - Celery 在 django 1.11 和 celery 4.0.0 或 4.1.0 中不会发现共享任务

python - 如何解决启动 Chrome 时出现 'Getting Default Adapter failed' 错误并尝试使用 Selenium 使用 ChromeDriver 访问网页

python - 使用 Python 更改没有类型的 <input> 标签的值

python - 如何设置只有整数的 x 轴刻度线?

python - 使用 celery,将 async_apply 发送到特定的虚拟主机?

python - C++ - 为现有库创建 Pythonwrapper

python - 通过连接多个文件创建的文本文件的交互式 3D 散点图