Celery task 执行报错 TypeError: Object of type set is not JSON serializable 问题分析处理

情况描述:

定义了新的shared_task推送到生产环境后,发现无法执行,会报错set对象无法序列化,报错内容如下:

python 复制代码
Traceback (most recent call last):
  File "/tmp/venv/lib64/python3.6/site-packages/kombu/serialization.py", line 50, in _reraise_errors
    yield
  File "/tmp/venv/lib64/python3.6/site-packages/kombu/serialization.py", line 221, in dumps
    payload = encoder(data)
  File "/tmp/venv/lib64/python3.6/site-packages/kombu/utils/json.py", line 70, in dumps
    **dict(default_kwargs, **kwargs))
  File "/tmp/venv/lib64/python3.6/site-packages/simplejson/__init__.py", line 412, in dumps
    **kw).encode(obj)
  File "/tmp/venv/lib64/python3.6/site-packages/simplejson/encoder.py", line 296, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/tmp/venv/lib64/python3.6/site-packages/simplejson/encoder.py", line 378, in iterencode
    return _iterencode(o, 0)
  File "/tmp/venv/lib64/python3.6/site-packages/kombu/utils/json.py", line 59, in default
    return super(JSONEncoder, self).default(o)
  File "/tmp/venv/lib64/python3.6/site-packages/simplejson/encoder.py", line 273, in default
    o.__class__.__name__)
TypeError: Object of type set is not JSON serializable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tmp/venv/lib64/python3.6/site-packages/celery/app/trace.py", line 449, in trace_task
    uuid, retval, task_request, publish_result,
  File "/tmp/venv/lib64/python3.6/site-packages/celery/backends/base.py", line 149, in mark_as_done
    self.store_result(task_id, result, state, request=request)
  File "/tmp/venv/lib64/python3.6/site-packages/celery/backends/base.py", line 342, in store_result
    request=request, **kwargs)
  File "/tmp/venv/lib64/python3.6/site-packages/django_celery_results/backends/database.py", line 19, in _store_result
    content_type, content_encoding, result = self.encode_content(result)
  File "/tmp/venv/lib64/python3.6/site-packages/django_celery_results/backends/database.py", line 52, in encode_content
    content_type, content_encoding, content = self._encode(data)
  File "/tmp/venv/lib64/python3.6/site-packages/celery/backends/base.py", line 297, in _encode
    return dumps(data, serializer=self.serializer)
  File "/tmp/venv/lib64/python3.6/site-packages/kombu/serialization.py", line 221, in dumps
    payload = encoder(data)
  File "/usr/lib64/python3.6/contextlib.py", line 99, in __exit__
    self.gen.throw(type, value, traceback)
  File "/tmp/venv/lib64/python3.6/site-packages/kombu/serialization.py", line 54, in _reraise_errors
    reraise(wrapper, wrapper(exc), sys.exc_info()[2])
  File "/tmp/venv/lib64/python3.6/site-packages/vine/five.py", line 194, in reraise
    raise value.with_traceback(tb)
  File "/tmp/venv/lib64/python3.6/site-packages/kombu/serialization.py", line 50, in _reraise_errors
    yield
  File "/tmp/venv/lib64/python3.6/site-packages/kombu/serialization.py", line 221, in dumps
    payload = encoder(data)
  File "/tmp/venv/lib64/python3.6/site-packages/kombu/utils/json.py", line 70, in dumps
    **dict(default_kwargs, **kwargs))
  File "/tmp/venv/lib64/python3.6/site-packages/simplejson/__init__.py", line 412, in dumps
    **kw).encode(obj)
  File "/tmp/venv/lib64/python3.6/site-packages/simplejson/encoder.py", line 296, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/tmp/venv/lib64/python3.6/site-packages/simplejson/encoder.py", line 378, in iterencode
    return _iterencode(o, 0)
  File "/tmp/venv/lib64/python3.6/site-packages/kombu/utils/json.py", line 59, in default
    return super(JSONEncoder, self).default(o)
  File "/tmp/venv/lib64/python3.6/site-packages/simplejson/encoder.py", line 273, in default
    o.__class__.__name__)
kombu.exceptions.EncodeError: Object of type set is not JSON serializable

初步分析

a.考虑是task调用过程中,参数传输过程异常,导致参数无法序列化,因为set 就是{1,2} 这种格式的,命令行验证没问题
b.考虑是其他模块有异常,发现其他模块的定时任务能正常执行
c.在测试环境能完整复现,但是由于celery 的调用无法调试,无法得到有效的反馈
d.命令行直接执行task函数不会报错,所以函数逻辑应该没有问题
e.最终发现是结果集里面存在set集合,导致后续结果传输的过程中无法解析成json,至此问题确定

解决办法:

将set转换成其他数据结构

总结:

a.对于原先测试验证能通过,修改后无法验证通过的,要先找到变更的内容,修改后要验证
b.这次花费时间较长是因为认为原先验证过,认为后续小的修改没有影响,导致思路不对,修改代码要验证,不要过于自信
c.对于celery这种无法调试的调用过程,复现存在难度,那就需要从函数逻辑,返回结果里面找答案
d.可能会转换成json格式的数据,一定不能包含set集合
e.其他:@shared_task 注解的函数,会加载到内存里面,需要重启Celery才会重新加载
相关推荐
懒大王爱吃狼42 分钟前
Python教程:python枚举类定义和使用
开发语言·前端·javascript·python·python基础·python编程·python书籍
秃头佛爷2 小时前
Python学习大纲总结及注意事项
开发语言·python·学习
待磨的钝刨2 小时前
【格式化查看JSON文件】coco的json文件内容都在一行如何按照json格式查看
开发语言·javascript·json
深度学习lover3 小时前
<项目代码>YOLOv8 苹果腐烂识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·苹果腐烂识别
API快乐传递者4 小时前
淘宝反爬虫机制的主要手段有哪些?
爬虫·python
阡之尘埃6 小时前
Python数据分析案例61——信贷风控评分卡模型(A卡)(scorecardpy 全面解析)
人工智能·python·机器学习·数据分析·智能风控·信贷风控
丕羽9 小时前
【Pytorch】基本语法
人工智能·pytorch·python
bryant_meng9 小时前
【python】Distribution
开发语言·python·分布函数·常用分布
m0_5945263010 小时前
Python批量合并多个PDF
java·python·pdf
工业互联网专业11 小时前
Python毕业设计选题:基于Hadoop的租房数据分析系统的设计与实现
vue.js·hadoop·python·flask·毕业设计·源码·课程设计