本文介绍了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.com'中的主机名,
'yahoo.com',
'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])

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



提前谢谢。

解决方案

为此,您可以使用实现处理程序。

 从芹菜导入任务

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.com',
                                                  'yahoo.com',
                                                  '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芹菜:如果有异常,则检索任务参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 01:45