-
Notifications
You must be signed in to change notification settings - Fork 103
Intro to Flask
tsungtwu edited this page Apr 19, 2017
·
1 revision
pip install Flask
.
|──────app/
| |────__init__.py
| |────api/
| | |────__init__.py
| | |────cve/
| | |────tweet/
| |──────config.Development.cfg
| |──────config.Production.cfg
| |──────config.Testing.cfg
| |────model/
| |────util/
|──────run.py
|──────tests/
| |──────test_cve.py
| |──────test_twitter.py
| |──────testData/
Example
app = Flask(__name__)
app.config['DEBUG'] = True
Example Usage
app = Flask(__name__, )
app.config.from_pyfile('application.cfg')
application.cfg example
#Flask settings
DEBUG = True # True/False
SERVER_NAME = 'localhost:5000'
JSON_AS_ASCII = 'false'
#elasticsearch settings
ELASTICSEARCH_HOST = 'http://localhost'
ELASTICSEARCH_PORT = '9200'
The following configuration values are used internally by Flask:
Using the simple app.run() from within Flask creates a single synchronous server on a single thread capable of serving only one client at a time. It is intended for use in controlled environments with low demand (i.e. development, debugging) for exactly this reason.
Spawning threads and managing them yourself is probably not going to get you very far either, because of the Python GIL.
That said, you do still have some good options. Gunicorn is a solid, easy-to-use WSGI server that will let you spawn multiple workers (separate processes, so no GIL worries), and even comes with asynchronous workers that will speed up your app (and make it more secure) with little to no work on your part (especially with Flask).
Still, even Gunicorn should probably not be directly publicly exposed. In production, it should be used behind a more robust HTTP server; nginx tends to go well with Gunicorn and Flask.
Reference
- Flask + Gunicorn + Nginx 部署
- https://hostpresto.com/community/tutorials/deploy-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04/
- Kickstarting Flask on Ubuntu - Setup and Deployment
- http://koko.ntex.tw/wordpress/djangopython-deploy-django-on-ubuntu-install-gunicorn-nginx/
- https://hostpresto.com/community/tutorials/deploy-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04/
- https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
- http://igordavydenko.com/talks/ua-pycon-2012.pdf
- Flask Testing
http://pythonhosted.org/Flask-Testing/ ES unit test example : https://github.com/pfig/flask-elasticsearch
Offical Website
Tutorial