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'\x80\x02}q\x00(X\x04\x00\x00\x00taskq\x01X-\x00\x00\x00celery_tasks.tasks.send_register_active_emailq\x02X\x02\x00\x00\x00idq\x03X$\x00\x00\x00e725345b-b943-404a-83d4-85ad04008439q\x04X\x04\x00\x00\x00argsq\x05X\x10\x00\x00\x00lifeoooo@163.comq\x06X\x07\x00\x00\x00xxx2212q\x07X\xad\x00\x00\x00eyJhbGciOiJIUzUxMiIsImlhdCI6MTU1Nzk3ODc3MSwiZXhwIjoxNTU3OTgyMzcxfQ.eyJjb25maXJtIjoyNn0._8gAN9YjorcgAPOso-Xd4W-LyJxCtXYyjJRpD8XdguA5dXgrfBILBj37xcN3I0IAmujhY9JLKq_H1T6mNHCz3wq\x08\x87q\tX\x06\x00\x00\x00kwargsq\n}q\x0bX\x07\x00\x00\x00retriesq\x0cK\x00X\x03\x00\x00\x00etaq\rNX\x07\x00\x00\x00expiresq\x0eNX\x03\x00\x00\x00utcq\x0f\x88X\t\x00\x00\x00callbacksq\x10NX\x08\x00\x00\x00errbacksq\x11NX\t\x00\x00\x00timelimitq\x12NN\x86q\x13X\x07\x00\x00\x00tasksetq\x14NX\x05\x00\x00\x00chordq\x15Nu.' (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)