Difference between revisions of "Short Notes on Flask and Flask-RestPlus"
From PaskvilWiki
(Created page with "== Structuring the Project == == Handling Requests == == File Upload == <pre>@ns.route('/') class Handle(Resource): @ns.param('data', description='We will just return t...") |
|||
Line 13: | Line 13: | ||
f.write(request.files['file'].read()) | f.write(request.files['file'].read()) | ||
return {'data': request.form['data']}</pre> | return {'data': request.form['data']}</pre> | ||
+ | |||
+ | == "MySQL server has gone away" with (Flask-)SQLAlchemy == | ||
+ | |||
+ | On infrequently used connections, most cloud hosts kill unused DB connections after some time. | ||
+ | |||
+ | This is a problem if your SQLAlchemy connection pool is not recycled often enough. | ||
+ | |||
+ | You can just lower the recycle time to solve this. It is also good to increase the pool size to handle the case where your load increases (at times): | ||
+ | |||
+ | <pre>app.config['SQLALCHEMY_POOL_SIZE'] = 100 | ||
+ | app.config['SQLALCHEMY_POOL_RECYCLE'] = 240</pre> | ||
+ | |||
+ | Default is <code>size = 10, recycle = 7200</code>, but many cloud providers kill inactive connections after about 5 min, thus the 4 min period above. |
Latest revision as of 08:20, 4 September 2019
Contents
Structuring the Project
Handling Requests
File Upload
@ns.route('/') class Handle(Resource): @ns.param('data', description='We will just return this.', _in='formData', type='string', required=True) @ns.param('file', description='We will save this as /tmp/test.jpg.', _in='formData', type='file', required=True) def post(self): with open('/tmp/test.jpg', 'wb') as f: f.write(request.files['file'].read()) return {'data': request.form['data']}
"MySQL server has gone away" with (Flask-)SQLAlchemy
On infrequently used connections, most cloud hosts kill unused DB connections after some time.
This is a problem if your SQLAlchemy connection pool is not recycled often enough.
You can just lower the recycle time to solve this. It is also good to increase the pool size to handle the case where your load increases (at times):
app.config['SQLALCHEMY_POOL_SIZE'] = 100 app.config['SQLALCHEMY_POOL_RECYCLE'] = 240
Default is size = 10, recycle = 7200
, but many cloud providers kill inactive connections after about 5 min, thus the 4 min period above.