celery 报错: Refusing to deserialize untrusted content of type pickle (application/x-python-serialize)

2023-02-18 13:35:44 浏览数 (3)

出错环境:

  • windows 10
  • python 3.6.8
  • django 1.11.26
  • celery 4.3.0

错误日志:

    同样的代码在 Mac 和 centos 环境下无此错误

代码语言:javascript复制
[2019-12-16 11:26:23,480: CRITICAL/MainProcess] Can't decode message body: ContentDisallowed('Refusing to deserialize untrusted content of type pickle (application/x-python-serialize)',) [type:'application/x-python-serialize' encoding:'binary' headers:{}]

body: b'x80x02}qx00(Xx04x00x00x00taskqx01Xx1fx00x00x00apps.share.tasks.post_to_beiqiaqx02Xx02x00x00x00idqx03X$x00x00x003f15b89f-23b5-4fae-aeb1-09dc203f4002qx04Xx04x00x00x00argsqx05]qx06Xx06x00x00x00kwargsqx07}qx08Xx07x00x00x00retriesqtKx00Xx03x00x00x00etaqnNXx07x00x00x00expiresqx0bNXx03x00x00x00utcqx0cx88Xtx00x00x00callbacksqrNXx08x00x00x00errbacksqx0eNXtx00x00x00timelimitqx0fNNx86qx10Xx07x00x00x00tasksetqx11NXx05x00x00x00chordqx12Nu.' (274b)
Traceback (most recent call last):
  File "e:py_virtualenvjoyoolibsite-packagesceleryworkerconsumerconsumer.py", line 546, in on_task_received
    type_ = message.headers['task']                # protocol v2
KeyError: 'task'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:py_virtualenvjoyoolibsite-packagesceleryworkerconsumerconsumer.py", line 551, in on_task_received
    payload = message.decode()
  File "e:py_virtualenvjoyoolibsite-packageskombumessage.py", line 193, in decode
    self._decoded_cache = self._decode()
  File "e:py_virtualenvjoyoolibsite-packageskombumessage.py", line 198, in _decode
    self.content_encoding, accept=self.accept)
  File "e:py_virtualenvjoyoolibsite-packageskombuserialization.py", line 253, in loads
    raise self._for_untrusted_content(content_type, 'untrusted')
kombu.exceptions.ContentDisallowed: Refusing to deserialize untrusted content of type pickle (application/x-python-serialize)

解决方法:

settings.py 配置(window 用 pickle 序列化)

代码语言:javascript复制
# celery 内容等消息的格式设置
if os.name != "nt":
    # Mac and Centos
    CELERY_ACCEPT_CONTENT = ['application/json', ]
    CELERY_TASK_SERIALIZER = 'json'
    # CELERY_RESULT_SERIALIZER = 'json'
else:
    # windows
    CELERY_ACCEPT_CONTENT = ['pickle', ]
    CELERY_TASK_SERIALIZER = 'pickle'
    # CELERY_RESULT_SERIALIZER = 'pickle'

0 人点赞