How can you catch a custom exception from Celery worker, or stop it being prefixed with `celery.backends.base`?

后端 未结 1 800
闹比i
闹比i 2021-02-15 12:33

My Celery task raises a custom exception NonTransientProcessingError, which is then caught by AsyncResult.get(). Tasks.py:

class NonTra         


        
相关标签:
1条回答
  • 2021-02-15 13:17
    import celery
    from celery import shared_task
    
    
    class NonTransientProcessingError(Exception):
        pass
    
    
    class CeleryTask(celery.Task):
    
        def on_failure(self, exc, task_id, args, kwargs, einfo):
            if isinstance(exc, NonTransientProcessingError):
                """
                deal with NonTransientProcessingError
                """
                pass
    
        def run(self, *args, **kwargs):
            pass
    
    
    @shared_task(base=CeleryTask)
    def add(x, y):
        raise NonTransientProcessingError
    

    Use a base Task with on_failure callback to catch custom exception.

    0 讨论(0)
提交回复
热议问题