Python芹菜:如果有异常,则检索任务参数

编程入门 行业动态 更新时间:2024-10-10 00:24:10
本文介绍了Python芹菜:如果有异常,则检索任务参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我开始使用Celery和Python,而且我有一个很简单的问题,但我似乎无法找到合适的答案...

如果我有一堆任务,其中一个抛出异常,是否有一种方法来检索传递给该任务的参数?

例如,如果我想获得一些主机名的IP,并且我创建一个任务...

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

...可以抛出异常,是否有一种获取主机名的价值的方法

让我们分组如下任务:

ip_subtasks = group ( resolve_hostname.s(hostname)为['google'中的主机名,'yahoo','failure.kommm'] )()

最后一个(尝试解决 failure.kommm )将引发异常。我想将芹菜任务的 get()方法放在 try / catch 中,并显示消息说尝试解决fail.kommm 时出现问题(如下所示):

对于ip_subtask在ip_subtasks:尝试:主机名,ips = ip_subtask.get(超时= 45)除了dns.exception.DNSException,e:#我知道这工作: logger.exception(尝试解决%s时发生的事情%ip_subtask.args [0])

所以,这就是问题...有没有办法检索一个任务执行的参数,如果我有任务实例本身?

提前谢谢。

解决方案

为此,您可以使用摘要类实现 on_failure 处理程序。

从芹菜导入任务 class DebugTask(Task): abstract = True def on_failure(self,exc ,task_id,args,kwargs,einfo): logger.exception(尝试解决%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) 参数: exc - 任务引发的异常。 task_id - 失败任务的唯一ID。 args - 失败的任务的原始参数。 kwargs - 失败的任务的原始关键字参数。 einfo - 包含追溯的ExceptionInfo实例。

I am getting started with Celery and Python, and I have a question that is probably very simple, but I don't seem to be able to find any suitable answer around...

If I have a bunch of tasks, and one of them throws, an exception, is there a way of retrieving the arguments that were passed to said task?

For instance, if I want to get the IPs some hostnames resolve to, and I create a task...

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

... which can throw an exception, is there a way of getting the value of that hostname argument outside the call when that Exception happens?

Let's say I group the tasks like:

ip_subtasks = group( resolve_hostname.s(hostname) for hostname in ['google', 'yahoo', 'failure.kommm'] )()

The last one (that tries to resolve failure.kommm ) will raise an exception. I'd like to put the get() method of the celery task inside a try/catch, and show a message saying Something went wrong when trying to resolve failure.kommm (something like shown below):

for ip_subtask in ip_subtasks: try: hostname, ips = ip_subtask.get(timeout=45) except dns.exception.DNSException, e: # I WISHED THIS WORKED: logger.exception("Something happened when trying" " to resolve %s" % ip_subtask.args[0])

So, that's the question... Is there a way of retrieving the arguments a task was executed with if I have the task instance itself?

Thank you in advance.

解决方案

To do this you can use an abstract class to implement an on_failure handler.

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)})

From the docs:

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芹菜:如果有异常,则检索任务参数

本文发布于:2023-10-30 18:49:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543693.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:芹菜   异常   参数   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!