芹菜异常处理 [英] Celery Exception Handling

查看:53
本文介绍了芹菜异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有此任务定义:

def some_other_foo(input)
raise Exception('This is not handled!')
return input

@app.task(
  bind=True,
  max_retries=5,
  soft_time_limit=20)
def some_foo(self, someInput={}):
   response=""
   try:
     response = some_other_foo(someInput)
   except Exception as exc:
     self.retry(countdown=5, exc=exc)
     response="error"
 return response

我有一个问题,那就是some_foo中没有处理异常,我得到的是错误而不是response ="error",任务崩溃了,并且我得到了Traceback,表明引发了异常.

I have a problem that exception is not handled in some_foo, I get error instead of response="error", task is crashed and i get Traceback that indicates an Exception was raised.

是否可以返回常规响应,但是将芹菜任务设置为失败,从而导致开花失败?

Is it possible to return regular response, but to set celery task as failed, so result in flower will be failed ?

我正在使用:
芹菜4.1
AMPQ作为经纪人
芹菜花作为监测

I am using:
Celery 4.1
AMPQ as broker
Celery Flower as monitoring

推荐答案

尝试\,但可以正常工作.您的任务将永远不会成功,因为您在 return 之前调用了 self.retry .让我们做一点测试:

Try \ except works fine. Your task will always be unsuccessful because you called self.retry before return. Let's make a little test:

from celery import Celery

app = Celery(name_app,broker_settings_etc....)

def some_other_foo(value):
    raise Exception('This is not handled!')

@app.task(
  bind=True,
  max_retries=5,
  soft_time_limit=20)
def some_foo(self):
    response = ""

    try:
        response = some_other_foo('test')
    except Exception as exc:
        self.retry(countdown=5, exc=exc)
        response = "error"

    return response

运行celery应用并调用我们的任务.您将在celery日志中看到如下内容:

Run celery app and call our task. You will see in celery logs something like this:

3fb-81de-e4149fa88f4c] retry: Retry in 5s: Exception('This is not handled!',)
[2017-08-18 15:50:34,160: INFO/MainProcess] Received task: tasks.some_foo[b656731b-c85d-43fb-81de-e4149fa88f4c] eta:[2017-08-18 12:50:39.156912+00:00]
[2017-08-18 15:50:34,161: INFO/MainProcess] Task tasks.some_foo[b656731b-c85d-43fb-81de-e4149fa88f4c] retry: Retry in 5s: Exception('This is not handled!',)
[2017-08-18 15:50:39,511: ERROR/MainProcess] Task tasks.some_foo[b656731b-c85d-43fb-81de-e4149fa88f4c] raised unexpected: Exception('This is not handled!',)
Traceback (most recent call last): 
# trace here...
Exception: This is not handled!

它如何工作.您为任务 max_retries = 5 设置了.当您调用 self.retry(countdown = 5,exc = exc)时,芹菜会中断任务处理并尝试使用 countdown (在我们的示例中为5)重新启动任务.经过5次尝试( max_retries ),芹菜不会重新运行任务.

How it works. You set for the task max_retries=5. When you called self.retry(countdown=5, exc=exc) Celery interrupts processing of task and try to restart task with countdown(in our case = 5). After 5 attempts(max_retries) Celery won't rerun task.

现在,让我们将 try \除了块更改为:

Now, let's change our try \ except block to:

try:
    response = some_other_foo('test')
except Exception:
    print 'handled'
    response = "bad response"

重新启动Celery并运行我们的任务.让我们检查一下日志:

Restart Celery and run our task. Let's check log:

[2017-08-18 15:58:41,893: INFO/MainProcess] Received task: tasks.some_foo[1437e7ce-1c69-4042-824b-5602f486c025]
[2017-08-18 15:58:41,895: WARNING/Worker-3] handled
[2017-08-18 15:58:41,896: INFO/MainProcess] Task tasks.some_foo[1437e7ce-1c69-4042-824b-5602f486c025] succeeded in 0.00186271299026s: 'bad response'

如您所见,处理程序工作正常.

As you can see handler works fine.

所以,总结一下.如果您调用 self.retry ,Celery将中断任务处理并尝试重新启动当前任务.

So, summarize. If you call self.retry, Celery will interrupt processing of task and try to restart a current task.

这篇关于芹菜异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆