您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python celery:如果存在异常,则检索任务参数

Python celery:如果存在异常,则检索任务参数

为此,您可以使用抽象类来实现on_failure处理程序。

from celery import Task

class DebugTask(Task):
    abstract = True

    def on_failure(self, exc, task_id, args, kwargs, einfo):
        logger.exception("Something happened when trying"
                         " to resolve %s" % args[0])

@tasks_app.task(base=DebugTask)
def resolve_hostname(hostname):
    return (hostname, {hst.address for hst in dns.resolver.query(hostname)})

从文档:

on_failure(self, exc, task_id, args, kwargs, einfo)

Parameters: 
  exc     – The exception raised by the task.
  task_id – Unique id of the Failed task.
  args    – Original arguments for the task that Failed.
  kwargs  – Original keyword arguments for the task that Failed.
  einfo   – ExceptionInfo instance, containing the traceback.
python 2022/1/1 18:45:56 有337人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶