gpt4 book ai didi

mysql - (2006, 'MySQL server has gone away') 的解决方法?

转载 作者:行者123 更新时间:2023-11-29 05:16:11 41 4
gpt4 key购买 nike

这个问题正是我的问题

Django - OperationalError: (2006, 'MySQL server has gone away')

解决这个未解决问题的明显方法是增加 wait_timeout执行

背景

我有一个 celery 任务,每天在特定时间运行一次。最初它工作正常但从上周开始我开始得到:

Exception_ocoured_: (2013, 'Lost connection to MySQL server during query')

这个 celery 任务只是从数据库中获取一些详细信息,最多 4000 行并发送给最终用户。

问题:

有什么方法可以仅针对在 django 环境中面临此问题的特定 celery 任务增加此超时,因为我不想打扰 native 设置?

我正在寻找一个 djangoish 解决方案,其生命周期仅与此 celery 任务执行时间相同。

例如:

@task
def doSomething():
try:
set_timeout_for_mysql = 20000 # <== main agenda for this question
# OR
ping_resp = somehow_test_mysql_con()
while(ping_resp == False):
keep trying to connect or create new connection
# do_operations
except Exception, e:
# log exception

规范:

In [18]: django.VERSION
Out[18]: (1, 7, 7, 'final', 0)

django-celery==3.0.21

附言:如果有人在不影响核心设置的情况下解决了这个问题,任何其他解决方法都可以!

最佳答案

from django.db import close_old_connections

...
close_old_connections()
... # do some db jobs, it will reconnect db

祝你好运

关于mysql - (2006, 'MySQL server has gone away') 的解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32437183/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com