zoukankan      html  css  js  c++  java
  • peewee在flask中的配置

    # 原文: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
    
    
    
  • 相关阅读:
    【转】#pragma pack(push,1)与#pragma pack(1)的区别
    emwin 之变量定义位置
    【转】C语言字符串与数字相互转换
    【转】用emWin进度条控件做个表盘控件,效果不错
    emwin 之消息 WM_INIT_DIALOG
    emwin 之使用键盘数据发送函数的注意事项
    emwin 解决在A窗口上新建B窗口后‘只激活’B窗口问题
    【转】数学与编程——求余、取模运算及其性质
    hdu4831 Scenic Popularity(线段树)
    2014年百度之星程序设计大赛
  • 原文地址:https://www.cnblogs.com/lajiao/p/10434501.html
Copyright © 2011-2022 走看看