How to make a celery task fail from within the task?

后端 未结 3 980
忘掉有多难
忘掉有多难 2021-02-05 00:37

Under some conditions, I want to make a celery task fail from within that task. I tried the following:

from celery.task import task
from celery import states

@t         


        
3条回答
  •  庸人自扰
    2021-02-05 01:21

    I got an interesting reply on this question from Ask Solem, where he proposes an 'after_return' handler to solve the issue. This might be an interesting option for the future.

    In the meantime I solved the issue by simply returning a string 'FAILURE' from the task when I want to make it fail and then checking for that as follows:

    result = AsyncResult(task_id)
    if result.state == 'FAILURE' or (result.state == 'SUCCESS' and result.get() == 'FAILURE'):
        # Failure processing task 
    

提交回复
热议问题