• how to create a flask server


    1. use database

    2. use redis

    3. inport/export excel2007 version+  

    from flask import send_from_directory
    import openpyxl
    # not xlwt or xlrd

    4. how to debug

    5. how to deploy

    6. pylint rules 参见

    http://pylint-messages.wikidot.com/messages:c0111 

     7. create a virtualenv

    $ virtualenv venv
    $ source bin/activate
    venv]$ pip install -r requirements.txt
    venv]$ deactivate

     8. flask-sqlalchemy paginate

    from flask_sqlalchemy import SQLAlchemy
    app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://user:password@host:port/database?charset=utf8mp4' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db_session = SQLAlchemy(app).session obj = db_session.quert(MODEL).filter(...).paginate(page=2, per_page=10) obj.total obj.pages obj.items

    问题1:

    ORM使用原生SQLAlchemy时报错。

    官网参考地址 http://docs.sqlalchemy.org/en/latest/dialects/mysql.html#connection-timeouts 

    使用姿势如下:

    from sqlalchemy import create_engine
    from sqlalchemy.orm import scoped_session, sessionmaker
    
    def create_app(conf_name=None):
        app = Flask(__name__)
        ......
        db_session = scoped_session(sessionmaker(
            autocommit=False, autoflush=False,
            bind=create_engine('mysql+pymysql://{USER}:{PASSWORD}@{HOST}:{PORT}/{DATABASE}?charset=utf8mb4',
                convert_unicode=True)))

    报错如下:

    OperationalError: (pymysql.err.OperationalError) (2006, "MySQL server has gone away (error(32, 'Broken pipe'))")
    
    or
    
    OperationalError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query')

    解决办法如下:

    阅读源码 ./python2.7/site-packages/sqlalchemy/engine/__init__.py 发现

        :param pool_recycle=-1: this setting causes the pool to recycle
            connections after the given number of seconds has passed. It
            defaults to -1, or no timeout. For example, setting to 3600
            means connections will be recycled after one hour. Note that
            MySQL in particular will disconnect automatically if no
            activity is detected on a connection for eight hours (although
            this is configurable with the MySQLDB connection itself and the
            server configuration as well).

    查看MySQL timeout 配置为默认值8小时

    mysql> show variables like '%timeout%';
    +-----------------------------+----------+
    | Variable_name               | Value    |
    +-----------------------------+----------+
    | connect_timeout             | 10       |
    | delayed_insert_timeout      | 300      |
    | have_statement_timeout      | YES      |
    | innodb_flush_log_at_timeout | 1        |
    | innodb_lock_wait_timeout    | 50       |
    | innodb_rollback_on_timeout  | OFF      |
    | interactive_timeout         | 28800    |
    | lock_wait_timeout           | 31536000 |
    | net_read_timeout            | 30       |
    | net_write_timeout           | 60       |
    | rpl_stop_slave_timeout      | 31536000 |
    | slave_net_timeout           | 3600     |
    | thread_pool_idle_timeout    | 60       |
    | wait_timeout                | 28800    |
    +-----------------------------+----------+
    14 rows in set (0.00 sec)

    所以在create_engine时新增参数 pool_recycle=3600。

    至此 问题修复。

     

     

  • 相关阅读:
    将最大主机/ DNS名称字符长度从63增加到255
    e3 cpu
    项目结构图
    Nyquist–Shannon sampling theorem 采样定理
    提高比特率 有损 无损 Video-and-Audio-file-format-conversion 视频声音转码
    比特率计算
    外微分
    功与路径无关的条件
    14.10.4 Defragmenting a Table 整理表
    14.10.4 Defragmenting a Table 整理表
  • 原文地址:https://www.cnblogs.com/tangkaixin/p/6843694.html
Copyright © 2020-2023  润新知