Search code examples
djangocelery

Run a function when celery task correctly ends in Django


I'm using Django and Celery and I'm correctly executing Celery tasks with

@shared_task(bind=True)
def start_serial_acquisition(self, idObj, porta_COM):

and

start_serial_acquisition.delay(instance.pk, porta_COM)

I would like to call or execute a specific function when a celery task ends with the SUCCESS flag.

Are there any decorators like Django @receiver(pre/post,) as I can't find something similar

I need to be executed after the tasks end so, a solution that launch the function as last line of the celery task is not suitable for me


Solution

  • It turned out that Celery has this kind of signals as documentation explains

    from celery.signals import task_success
    
    @task_success.connect
    def task_completed_handler(sender=None, headers=None, body=None, **kwargs):
        print(sender.request.id)