[英]Long celery task causes MySQL timeout in Django - options?
I have a celery task which takes about 6 hours. 我有芹菜任务,大约需要6个小时。 At the end of it, Django (or possibly Celery) raises an exception "MySQL server has gone away".
最后,Django(或可能是Celery)提出异常“MySQL服务器已经消失”。
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;
我(我认为没有)控制ping或其他任务; 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? 我可以在函数中调用重新建立连接吗?
(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.) (我已经使用相同的RDS MySQL数据库“本地”运行此任务而没有问题,但是我在AWS实例上运行时得到它。)
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. 关键是close_old_connections调用 - 否则ensure_connection将不起作用。
Ian 伊恩
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.