Skip to content Skip to sidebar Skip to footer

Long Celery Task Causes MySQL Timeout In Django - Options?

I have a celery task which takes about 6 hours. At the end of it, Django (or possibly Celery) raises an exception 'MySQL server has gone away'. After doing some reading, it appear

Solution 1:

Eventually found what appears to have worked:

from django.db import close_old_connections
import time

def check_and_retry_django_db_connection():
    close_old_connections()

    db_conn = False
    while not db_conn:
        try:
            connection.ensure_connection()
            db_conn = True
        except OperationalError:
            print('Database unavailable, waiting 1 second...')
            time.sleep(1)

    print('Database available')

The key is the close_old_connections call - ensure_connection will not work otherwise.

Ian


Post a Comment for "Long Celery Task Causes MySQL Timeout In Django - Options?"