# 原文:https://blog.csdn.net/mouday/article/details/85332510
Flask的钩子函数与peewee.InterfaceError: (0, '')
2018年12月28日 22:56:07 彭世瑜 阅读数:70更多
个人分类: flask
版权声明:本文为博主原创文章,欢迎转载,请注明出处 https://blog.csdn.net/mouday/article/details/85332510
问题
使用flask搭了一个服务,用到了peewee模块,运行时间长了就报错 peewee.InterfaceError: (0, '')
1
百度上一搜,发现有自己的文章 peewee: OperationalError: (2006, ‘MySQL server has gone away’) 那个时候,处理的是peewee2版本的问题,如今又在处理peewee3的问题,真是问题多多 解决
查看peewee的issue,看到一个回到,给出两个方案
1、使用flask-peewee 模块
2、使用flask的钩子函数 尝试使用方案2:
request来的时候打开数据库连接,response返回的时候关闭数据库连接 根据文档给出的代码 from flask import Flask
from peewee import * database = SqliteDatabase('my_app.db')
app = Flask(__name__) # This hook ensures that a connection is opened to handle any queries
# generated by the request.
@app.before_request
def _db_connect():
database.connect() # This hook ensures that the connection is closed when we've finished
# processing the request.
@app.teardown_request
def _db_close(exc):
if not database.is_closed():
database.close() from flask import Flask
from peewee import * database = SqliteDatabase('my_app.db')
app = Flask(__name__) # This hook ensures that a connection is opened to handle any queries
# generated by the request.
@app.before_request
def _db_connect():
database.connect() # This hook ensures that the connection is closed when we've finished
# processing the request.
@app.teardown_request
def _db_close(exc):
if not database.is_closed():
database.close()
---------------------
作者:彭世瑜
来源:CSDN
原文:https://blog.csdn.net/mouday/article/details/85332510
版权声明:本文为博主原创文章,转载请附上博文链接!
参考: https://github.com/coleifer/peewee/issues/1546
http://docs.peewee-orm.com/en/latest/peewee/database.html#flask
http://docs.peewee-orm.com/en/latest/peewee/database.html#error-2006-mysql-server-has-gone-away

最新文章

  1. jdbc java数据库连接 10)批处理
  2. C#事件学习
  3. IOS9 Swift
  4. 关于if(a<b<c)判断的问题
  5. lucene搜索方式(query类型)
  6. 通过CSS禁用页面模块的复制和粘贴功能
  7. highcharts 结合phantomjs纯后台生成图片
  8. MetadataType的使用
  9. hdu 1034 Candy Sharing Game
  10. QT显示输出及其桌面
  11. HTML5 新功能
  12. Vigenère Cipher 维吉尼亚加解密算法
  13. Java依据Url下载图片
  14. JUnit——(二)注解
  15. 如何在IIS6,7中部署ASP.NET网站(转载)
  16. H3C无线路由器安装与设置
  17. SpringBoot入门:Hello World
  18. 构建web应用之——SpringMVC实现CRUD
  19. Biorhythms POJ - 1006 中国剩余定理
  20. 【POJ2230】Watchcow

热门文章

  1. Python2爬虫获取的数据存储到MySQL中时报错"Incorrect string value: '\\xE6\\x96\\xB0\\xE9\\x97\\xBB' for column 'new' at row 1"的解决办法
  2. 【转载】用C#编写一个简单的记事本
  3. P1278 单词游戏
  4. HTML、 CSS、 JavaScript三者的关系 1
  5. bzoj2165: 大楼 (矩阵快速幂)
  6. harbor1.4.0高可用部署
  7. C#基础-连接Access与SQL Server
  8. sed 用法 转https://www.cnblogs.com/Dev0ps/p/8441255.html
  9. 解决webstrom 输入法光标不跟随问题
  10. 一些实用的JQuery代码片段收集