gpt4 book ai didi

python - MySQLdb初始连接超时

转载 作者:太空狗 更新时间:2023-10-29 21:40:12 27 4
gpt4 key购买 nike

我在 python 中使用 MySQLdb 连接到远程数据库并执行查询。到目前为止,一切都很好。但是,由于多种原因,指定的 IP 地址可能根本没有 MySQL 服务器(或实际上没有任何服务器)。在这些情况下,此时 MySQLdb 无限期挂起。这并不理想,我更希望能够设置一个超时时间,如果超过该超时时间,我可以提出异常并继续我的生活。

有没有简单的方法可以做到这一点?

最佳答案

您可以将connect_timeout 参数传递给MySQLdb.connect 函数。引自 docs :

connect_timeout

Abort if connect is not completed within given number of seconds.

Default: no timeout (?)

关于python - MySQLdb初始连接超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18602836/

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