• Celery运行错误:拒绝反序列化pickle类型的不可信内容


    celery4.3运行时出现错误代码

    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(Xx04x00x00x00taskqx01X-x00x00x00celery_tasks.tasks.send_register_active_emailqx02Xx02x00x00x00idqx03X$x00x00x00e725345b-b943-404a-83d4-85ad04008439qx04Xx04x00x00x00argsqx05Xx10x00x00x00lifeoooo@163.comqx06Xx07x00x00x00xxx2212qx07Xxadx00x00x00eyJhbGciOiJIUzUxMiIsImlhdCI6MTU1Nzk3ODc3MSwiZXhwIjoxNTU3OTgyMzcxfQ.eyJjb25maXJtIjoyNn0._8gAN9YjorcgAPOso-Xd4W-LyJxCtXYyjJRpD8XdguA5dXgrfBILBj37xcN3I0IAmujhY9JLKq_H1T6mNHCz3wqx08x87q	Xx06x00x00x00kwargsq
    }qx0bXx07x00x00x00retriesqx0cKx00Xx03x00x00x00etaq
    NXx07x00x00x00expiresqx0eNXx03x00x00x00utcqx0fx88X	x00x00x00callbacksqx10NXx08x00x00x00errbacksqx11NX	x00x00x00timelimitqx12NNx86qx13Xx07x00x00x00tasksetqx14NXx05x00x00x00chordqx15Nu.' (505b)
    Traceback (most recent call last):
      File "/root/.virtualenvs/dailyfresh/lib/python3.6/site-packages/celery/worker/consumer/consumer.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 "/root/.virtualenvs/dailyfresh/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 551, in on_task_received
        payload = message.decode()
      File "/root/.virtualenvs/dailyfresh/lib/python3.6/site-packages/kombu/message.py", line 192, in decode
        self._decoded_cache = self._decode()
      File "/root/.virtualenvs/dailyfresh/lib/python3.6/site-packages/kombu/message.py", line 197, in _decode
        self.content_encoding, accept=self.accept)
      File "/root/.virtualenvs/dailyfresh/lib/python3.6/site-packages/kombu/serialization.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)

    原因没有配置对应的配置项

    from kombu import serialization
    serialization.registry._decoders.pop("application/x-python-serialize")
    
    app.conf.update(
        CELERY_ACCEPT_CONTENT = ['json'],
        CELERY_TASK_SERIALIZER = 'json',
        CELERY_RESULT_SERIALIZER = 'json',
    )

    运行成功

    完整代码

    # 使用celery
    from django.conf import settings
    from django.core.mail import send_mail
    from celery import Celery
    import time
    
    # 在任务处理者一端加这几句代码
    # centos上面
    # import os
    # import django
    # os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dailyfresh.settings')
    # django.setup()
    
    
    # 创建一个Celery类的实例对象
    app = Celery(include=['celery_tasks.tasks'],broker='redis://192.168.91.128:6379/8')
    
    from kombu import serialization
    serialization.registry._decoders.pop("application/x-python-serialize")
    
    app.conf.update(
        CELERY_ACCEPT_CONTENT = ['json'],
        CELERY_TASK_SERIALIZER = 'json',
        CELERY_RESULT_SERIALIZER = 'json',
    )
    #定义任务函数
    @app.task
    def send_register_active_email(to_email,username,token):
        '''发送激活邮件'''
        #组织邮件信息
        subject = "天天生鲜欢迎信息"
        message = ""  # 邮件正文
        sender = settings.EMAIL_FROM
        receiver = [to_email]  # 收件人邮箱列表
        html_message = '<h1>%s, 欢迎您成为天天生鲜注册会员</h1>请点击下面链接激活您的账户<br/><a href="http://127.0.0.1:8000/user/active/%s">http://127.0.0.1:8000/user/active/%s</a>' % (
        username, token, token)
        send_mail(subject, message, sender, receiver, html_message=html_message)
        time.sleep(7)
  • 相关阅读:
    0094-leetcode算法实现之二叉树中序遍历-binary-tree-inorder-traversal-python&golang实现
    0144-leetcode算法实现之二叉树的前序遍历-binary-tree-preorder-traversal-python&golang实现
    0347-leetcode算法实现之前K个高频元素-top-k-frequent-elements-python&golang实现
    0239-leetcode算法实现之滑动窗口最大值-sliding-window-maximum-python&golang实现
    0150-leetcode算法实现之逆波兰表达式-evaluate-reverse-polish-notation-python&golang实现
    1047-leetcode算法实现之删除字符串的所有相邻的重复字符-remove-all-adjacent-duplicates-in-string-python&golang实现
    pstack 追踪进程-转
    0225-leetcode算法实现之用队列实现栈-implement-stack-using-queues-python&golang实现
    0020-leetcode算法实现之有效括号-valid-parentheses-python&golang实现
    0232-leetcode算法实现-用栈实现队列-implement-queue-using-stacks-python&golang实现
  • 原文地址:https://www.cnblogs.com/xiaotaiyangi/p/10874882.html
Copyright © 2020-2023  润新知