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。
至此 问题修复。
how to create a flask server的更多相关文章
- Create a SQL Server Database on a network shared drive
(原文地址:http://blogs.msdn.com/b/varund/archive/2010/09/02/create-a-sql-server-database-on-a-network-sh ...
- How to create a PPPoE Server on Ubuntu? (Untested)
How to create a PPPoE Server on Ubuntu? March 30, 2011 coder_commenter Leave a comment Go to comment ...
- [转]How to: Create a Report Server Database (Reporting Services Configuration)
本文转自:https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms157300%28v%3dsql.10 ...
- 报错:Cannot create PoolableConnectionFactory (The server time zone value 'CST' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverT
报错:Cannot create PoolableConnectionFactory (The server time zone value 'CST' is unrecognized or repr ...
- How to create a jump server in AWS VPC
本来是写的Word文档,给其他国家的同时看的,所以一开始就是英文写的,也没打算翻译成为中文了,顺便抱怨下,网上资料找了很久的资料都没有看到介绍怎么在单机环境下搭建RD Gateway的,写本文的目的是 ...
- (转)Genymotion安装virtual device的“unable to create virtual device, Server returned Http status code 0”的解决方法
网络原因无法下载virtual device,status 为0表示服务器没有响应.FQ下载吧,有VPN的小伙伴推荐这种. 或者直接手动下载ova虚拟机文件,然后将虚拟机文件导入到virtualbox ...
- Genymotion加入模拟器时报“Unable to create virtual device,Server returned HTTP status code 0”
今天也遇到这个问题,算是对这个文章的一点补充 打开图中这个文件 C:\Users\xxx\AppData\Local\Genymobile 搜索 [downloadFile] 找到这个一串URL ht ...
- Error generating Swagger server (Python Flask) from Swagger editor
1down votefavorite http://stackoverflow.com/questions/36416679/error-generating-swagger-server-pyt ...
- Create Windows Server 2008 cluster from the command line
How to create a Windows Server 2008 cluster from the command line? Creating a cluster in Server 2008 ...
随机推荐
- echarts之bootstrap选项卡不能显示其他标签echarts图表
在echarts跟bootstrap选项卡整合的时候,默认第一个选中选项卡可以正常加载echarts图表,但是切换其他选项的时候不能渲染出其他选项卡echarts图表. 解决方法: 在js中添加代码: ...
- @vue/cli 3.x 版本配置productionGzip提高性能
第一步:安装插件 npm i -D compression-webpack-plugin 第二步:引入.在文件vue.config.js里导入compression-webpack-plugin,并添 ...
- 20175215 2018-2019-2 第三周java课程学习总结
第三周 一.使用JDB调试java代码(主要内容为断点) 以下文字内容转自使用JDB调试java程序,图片则为自己的截图 我们提倡在Linux命令行下学习Java编程.学习时在Ubuntu Bash中 ...
- 解决cron不执行的问题
在FreeBSD5.4下面做开发,需要定期备份mysql数据,开始在网上找了bash的脚本,但是执行无效,一怒之下,使用php来写,嘿嘿,其实php写脚本也不错滴.备份其实就是把mysql的数据库文件 ...
- leetcode-easy-string- 38 Count and Say
mycode 91.28% 思路:题意实在太难理解了,尤其是英文又不好,只能参看下别人的资料,理解下规则.终于理解,题意是n=1时输出字符串1:n=2时,数上次字符串中的数值个数,因为上次字符串有 ...
- kotlin之字符串模板
所谓字符串模板就是在字符串中添加若干个占位符,内容会在后期指定,也就是说,用模板可以设置字符串动态的部分,模板使用美元符号$设置如i=$i 中 的$i就是一个占位符,其中4后面的i是变量,随着i的变化 ...
- 使用conda安装命令时一直出现问题,因为从2019年4月添加的国内镜像都不能用了
安装过程中出现以下问题:(历尽千辛万苦,终于才查到原来是清华源,腾讯源都不能用了)The remote server could not find the noarch directory for t ...
- Redis 入门 3.1 热身
3.1 热身 1. 获得符合规则的键名列表 KEYS pattern pattern 支持 glob 风格通配符格式 语言 字符组 ? 匹配一个字符 * 匹配任意个(包括0个)字符 [] 匹配括号间的 ...
- 平衡树(fhq无旋treap)
fhq板子(代码正确且风格易懂) 洛谷P3369 #include<iostream> #include<cstring> #include<cstdio> #in ...
- Java 并发编程:volatile的使用及其原理(二)
一.volatile的作用 在<Java并发编程:核心理论>一文中,我们已经提到过可见性.有序性及原子性问题,通常情况下我们可以通过Synchronized关键字来解决这些个问题,不过如果 ...