Django Celery 在抛出自定义异常时出现酸洗错误

Django Celery getting pickling error on throwing custom exception

我在 Django 应用程序中使用芹菜。如果出现故障,我会提出一个自定义异常,该异常是用 class:

定义的
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": []
}

我在异常 class 中错误配置了什么?

解决方案是删除对 super(CustomException, self).__init__(self.message) 的调用。

据我了解,Pickling 需要一个只有 1 个参数的 init 函数。因此,为了避免错误,我要么必须更改 CustomException 的初始化以仅接受 1 个参数,要么删除对 super

的调用