Share a common utility function between Celery tasks

I have many tasks in Celery all related with help canvas chain.

@shared_task(bind=True)
def my_task_A(self):
    try:
        logger.debug('running task A')
        do something
    except Exception:
        run common cleanup function

@shared_task(bind=True)
def my_task_B(self):
    try:
        logger.debug('running task B')
        do something else
    except Exception:
        run common cleanup function

...

So far so good. The problem is that I am looking for best practice when it comes to using a general utility function, for example:

def cleanup_and_notify_user(task_data):
    logger.debug('task failed')
    send email
    delete folders
    ...

What is the best way to do this without blocking tasks? For example, can you just replace it run common cleanup functionwith a call cleanup_and_notify_user(task_data)? And what happens if several tasks from several employees try to call this function at the same time?

Does each employee have their own copy? Apparently I'm a little confused by a few concepts here. Any help is greatly appreciated.

Thanks to everyone in advance.

+4
1

celery python, , , . , , , , , , ,

+2

Source: https://habr.com/ru/post/1584647/


All Articles