长时间的芹菜任务导致Django中的MySQL超时-选项? [英] Long celery task causes MySQL timeout in Django - options?

查看:53
本文介绍了长时间的芹菜任务导致Django中的MySQL超时-选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的芹菜任务大约需要6个小时.最后,Django(或可能是Celery)提出了一个异常"MySQL服务器已消失".

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".

经过阅读后,看来这是长任务的已知问题.我(认为自己)无法控制ping或执行其他任务;但是在花费时间的调用完成后会引发异常(但仍在task函数中).

After doing some reading, it appears that this is a known issue with long tasks. I don't (think I have) control over pinging or otherwise mid-task; but the exception is raised after the call which takes time has finished (but still within the task function).

我可以在函数中进行调用以重新建立连接吗?

Is there a call I can make within the function to re-establish the connection?

(我已经使用相同的RDS MySQL DB在本地"运行了此任务,但没有出现问题,但是在AWS实例上运行时得到了它.)

(I have run this task "locally" with the same RDS MySQL DB and not had the issue, but I am getting it when running on an AWS instance.)

推荐答案

最终找到了行之有效的方法:

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')

关键是close_old_connections调用-确保否则连接将无法工作.

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

伊恩

这篇关于长时间的芹菜任务导致Django中的MySQL超时-选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆