[Django]-How to recover from Django Celery dropped connection to Postgres

3👍

I solved this by adding an after_return handler to all my celery tasks to close the DB connection after each execution.

http://docs.celeryproject.org/en/latest/userguide/tasks.html#abstract-classes

class DBTask(celery.Task):
    abstract = True

    def after_return(self, *args, **kwargs):
        connection.close()

@celery.task(base=DBTask)
def mytask():
    # Do Stuff with DB

Leave a comment