【问题标题】:Django Celery getting pickling error on throwing custom exceptionDjango Celery 在抛出自定义异常时出现酸洗错误
【发布时间】: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


【解决方案1】:

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

从我读到的 Pickling 期望一个只有 1 个参数的 init 函数。因此,为了避免错误,我要么必须更改我的 CustomException 的 init 以仅接受 1 个参数,要么删除对 super 的调用

【讨论】:

    猜你喜欢
    • 2017-12-02
    • 1970-01-01
    • 2019-06-02
    • 2011-10-06
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    相关资源
    最近更新 更多