gpt4 book ai didi

python - Django 不使用 CONN_MAX_AGE=60 重用与 MySQL 的连接

转载 作者:可可西里 更新时间:2023-11-01 09:02:37 41 4
gpt4 key购买 nike

我在开发中使用 Django 1.9.2 (DEBUG=True) 和 MySQL 5.6.23。下面是我的数据库设置

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname',
'USER': "django",
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
'CONN_MAX_AGE': 60,
}
}

我正在使用以下命令查询 MySQL 以获取事件连接数:

show status where `variable_name` = 'Threads_connected';

结果是这样的

+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_connected | 10 |
+-------------------+-------+
1 row in set (0,00 sec)

每次我向 Django 发出新请求时,连接线程的数量都会增加,直到我得到 (1040, 'Too many connections') 当数字 Threads_connected=151。此外,连接不会在 60 秒后关闭。

此行为似乎不会在生产环境中发生(DEBUG=False)。

最佳答案

您在开发模式下运行 Django,因此 CONN_MAX_AGE 无效,因为每个请求都由不同的线程处理。来自docs :

The development server creates a new thread for each request it handles, negating the effect of persistent connections. Don’t enable them during development

关于连接在 60 秒后未关闭:timed out connections are checked and closed when starting the next request ,所以只要在超过 60 秒后向 django 发出新的请求,它应该会检测到过时的连接并关闭它。

关于python - Django 不使用 CONN_MAX_AGE=60 重用与 MySQL 的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36837216/

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