python - Peewee MySQL 服务器已消失错误

标签 python peewee

有什么方法可以捕获此异常并重新初始化连接吗?

或者可能调整 peewee 以避免此异常。

最佳答案

MySQL 定义了空闲超时,超过该时间后,服务器将终止空闲连接。这种情况发生在长期的 peewee 连接上。

要在此错误(或其他几个相关错误)后自动尝试重新连接,您应该使用 playhouse.shortcuts 中的 ReconnectMixin 帮助器。

它定义了几个错误条件,应重新打开并重试连接:

from peewee import MySQLDatabase
from playhouse.shortcuts import ReconnectMixin

class ReconnectMySQLDatabase(ReconnectMixin, MySQLDatabase):
    pass

db = ReconnectMySQLDatabase('my_app', ...)

关于python - Peewee MySQL 服务器已消失错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45345549/

相关文章:

python - 将 SQL 子查询转换为 Peewee ORM

python - peewee:classname_set 为 json 数组

python - 为什么 Python http 请求创建 TIME_WAIT 连接?

python - "Begin"和 "End"基于连续值的日期

java - 与 Java 和 Python 相关的 XML 问题

python - 使用 peewee 进行不区分大小写的排序

python - 我能以某种方式查询 peewee/postgres 中的所有现有表吗?

python - PeeWee PostgreSQL 数据库创建 : does not exist

python - Pandas - 删除多列中的重复项

python - 未定义 DISPLAY 时使用 matplotlib 生成 PNG