gpt4 book ai didi

python - Peewee MySQL 服务器已经消失

转载 作者:可可西里 更新时间:2023-11-01 07:19:59 25 4
gpt4 key购买 nike

我使用 flask 和 peewee。有时 peewee 会抛出这个错误

MySQL server has gone away (error(32, 'Broken pipe'))

Peewee 数据库连接

db = PooledMySQLDatabase(database,**{
"passwd": password, "user": user,
"max_connections":None,"stale_timeout":None,
"threadlocals" : True
})

@app.before_request
def before_request():
db.connect()

@app.teardown_request
def teardown_request(exception):
db.close()

在出现“MySQL server has gone away (error(32, 'Broken pipe'))”的 mysql 错误后,选择查询可以正常工作,但插入、更新、删除查询不起作用。

插入、更新、删除查询在后面(在 mysql 中)工作,但 peewee 抛出此错误。

(2006, "MySQL server has gone away (error(32, 'Broken pipe'))")

最佳答案

peewee的文档已经讲过这个问题,链接在这里:Error 2006: MySQL server has gone away

This particular error can occur when MySQL kills an idle database connection. This typically happens with web apps that do not explicitly manage database connections. What happens is your application starts, a connection is opened to handle the first query that executes, and, since that connection is never closed, it remains open, waiting for more queries.

所以您在管理数据库连接方面遇到了一些问题。


因为我无法重现你的问题,你能不能试试这个,这样关闭你的数据库:

@app.teardown_appcontext
def close_database(error):
db.close()

您可能会从文档中获得一些信息:Step 3: Database Connections

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

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