gpt4 book ai didi

python - 在 Django 中捕获 OperationalError 1040

转载 作者:行者123 更新时间:2023-11-30 23:48:28 25 4
gpt4 key购买 nike

我在共享服务器上运行我的 Django 网站,因此我的用户有时会收到内部服务器错误 500 页面,这是由于特定的 OperationalError 异常将其自身标识为 (1040, "Too Many Connections")。我的 urls.py 中有一个自定义 500.html 页面和 handler500 = 'myapp.views.error500' -- error500 方法我的观点是:

def error500(request):
exctype, value = sys.exc_info()[:2]
msg = ''
if exctype == OperationalError:
msg = 'We\'re busy at the moment -- please reload this page in a little while.'
return render_to_response('500.html', {'msg': msg})

我从 MySQLdb 模块导入 OperationalError 的地方。但这会将所有 MySQL 错误归因于“太多连接”——我怎样才能只捕获 1040 错误?我怎样才能在不等待查看共享 MySQL 服务器是否在我碰巧浏览我自己的网站时过载的情况下对其进行测试?

最佳答案

这是对我有用的:

def error500(request):
exctype, value = sys.exc_info()[:2]
msg = ''
if exctype == OperationalError and value.args[0] == 1040:
msg = 'We\'re busy at the moment -- please reload this page in a little while.'
return render_to_response('500.html', {'msg': msg})

我通过编写一个 bash 脚本来测试它,以打开 151 个连接(最大)到我的 MySQL 服务器,然后访问我的页面以尝试打开第 152 个...

关于python - 在 Django 中捕获 OperationalError 1040,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25493419/

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