Try \ except 工作正常。你的任务永远不会成功,因为你在return 之前调用了self.retry。让我们做一个小测试:
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 日志中看到如下内容:
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) 时,Celery 会中断任务处理并尝试使用countdown(在我们的示例中为 5)重新启动任务。 5 次尝试后(max_retries) Celery 不会重新运行任务。
现在,让我们将 try \ except 块更改为:
try:
response = some_other_foo('test')
except Exception:
print 'handled'
response = "bad response"
重启 Celery 并运行我们的任务。让我们检查一下日志:
[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'
如您所见,处理程序工作正常。
所以,总结一下。如果你调用self.retry,Celery 将中断任务处理并尝试重新启动当前任务。