芹菜异常处理

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

假设我有此任务定义:

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.

更多推荐

芹菜异常处理

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

发布评论

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

>www.elefans.com

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