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。

至此 问题修复。

 

 

 

 

 

posted @ 2017-05-12 06:48  开心星  阅读(694)  评论(0编辑  收藏  举报