【发布时间】:2020-10-15 10:56:40
【问题描述】:
我在 django 应用程序中使用 celery。如果发生故障,我会引发一个自定义异常,该异常是使用此类定义的:
class CustomException(Exception):
def __init__(self, err_input, err_info=''):
self.err_key, self.err_msg, self.app_domain = err_input
self.message = self.err_msg
super(CustomException, self).__init__(self.message)
self.err_info = err_info
def __str__(self):
return '[{}] {}'.format(str(self.err_key), str(self.err_msg))
def __repr__(self):
return self.message
error_info 用于内部记录目的。
如果我抛出我在相关 celery 任务中得到的消息:
{
"task_id": "0dfd5ef3-0df1-11eb-b74a-0242ac110002",
"status": "FAILURE",
"result": {
"exc_type": "MaybeEncodingError",
"exc_message": [
"'PicklingError(\"Can\\'t pickle <class \\'module.CustomException\\'>: it\\'s not the same object as module.CustomException\")'",
"\"(1, <ExceptionInfo: CustomException('Error message')>, None)\""
],
"exc_module": "billiard.pool"
},
"traceback": null,
"children": []
}
我在我的异常类中错误配置了什么?
【问题讨论】:
-
到目前为止有什么解决方案吗?
-
是的,解决方案是删除对
super(CustomException, self).__init__(self.message)的调用。据我记得,我读到的 Pickling 期望一个只有 1 个参数的 init 函数。因此,为了避免错误,我要么必须更改我的CustomException的 init 以仅接受 1 个参数,要么删除对 super 的调用
标签: python django celery pickle