Flask is a miniature web development framework in Python. In debug mode or simple Uwsgi mode, flask is blocking mode, that is, only one request can be activated at a time, or the number of requests in the UWSGI open multi-process, in response to a known request; Here we use Uwsgi and gevent with Nginx to solve the flask blocking mode.
1. Environment
CentOS Linux release 7.0.1406 (Core)
Python 2.7.5
2. Installation Class Library
Yum install python-devel zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel Tk-devel GCC
3. Download and install Python package management software pip
View Python version
[Email protected] ~]# Python-vpython 2.7.5
Download pip-7.1.0.tar.gz
wget https://pypi.python.org/packages/source/p/pip/pip-7.1.0.tar.gz#md5=d935ee9146074b1d3f26c5f0acfd120e
Unzip the installation
Tar zxf pip-7.1.0.tar.gzcd pip-7.1.0python setup.py install## prompts for the following information, without error, indicating successful installation installed/usr/lib/python2.7/ site-packages/pip-7.1.0-py2.7.eggprocessing dependencies for pip==7.1.0finished processing dependencies for pip== 7.1.0
Download setuptools-18.1.tar.gz
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-18.1.tar.gz#md5=f72e87f34fbf07f299f6cb46256a0b06
Unzip the installation
Tar zxf setuptools-18.1.tar.gz cd Setuptools-18.1python setup.py install## prompts for the following information, without error, indicating successful installation installed/usr/lib/ python2.7/site-packages/setuptools-18.1-py2.7.eggprocessing Dependencies for setuptools==18.1finished processing Dependencies for setuptools==18.1
4. Install flask/uwsgi/gevent/ Nignx
Installing Flask
Pip install flask## prompts for the following information, without error, indicating successful installation successfully installed jinja2-2.8 MarkupSafe-0.23 Werkzeug-0.10.4 flask-0.10.1 itsdangerous-0.24
Installing Uwsgi
Pip install uwsgi## prompts for the following information, without error, indicating successful installation collecting Uwsgi Using cached uwsgi-2.0.11.1.tar.gzinstalling collected packages : Uwsgi Running setup.py Install for uwsgisuccessfully installed uwsgi-2.0.11.1
Installing Gevent
Pip install gevent## prompts the following information, without error, indicating successful installation Running setup.py install for Greenlet Running setup.py install for geventsucces Sfully installed gevent-1.0.2 greenlet-0.4.7
download and compile the installation Nginx
Useradd wwwwget Http://nginx.org/download/nginx-1.8.0.tar.gztar zxf nginx-1.8.0.tar.gz CD nginx-1.8.0./configure-- Prefix=/usr/local/nginx--user=www--group=www--with-http_stub_status_module--with-http_ssl_modulemakemake Install
5. New Flask app File/data/wwwroot/myapp.py:
[[email protected] ~]# mkdir/data/wwwroot-p[[email protected] ~]# Cd/data/wwwroot/[[email protected] wwwroot]# cat Myap P.pyfrom flask Import Flaskapp = Flask (__name__) @app. Route ('/') def hello_world (): Return ' Hello world! ' if __name__ = = ' __main__ ': App.run ()
6, configuration Uwsgi and Gevent
(1) Configuring the Uwsgi.ini file Vim/usr/local/nginx/conf/uwsgi.ini
[uwsgi] master = true vhost = true workers = 2 reload-mercy = 10 vacuum = true max-requests = 1000 Limit-as = 6048 chmod-socket = 666 socket = /tmp/uwsgi.sock chdir = /data/wwwroot/ # #flask app Catalogue module = myapp ## flask app name, which is our myapp.py callable = app touch-reload = /data/wwwroot/ pidfile = /var/run/uwsgi.pid Daemonize = /usr/local/nginx/logs/uwsgi.log gevent = 100 ## join  GEVENT = 100&NBSP, non-blocking mode
(2) Configure cat/data/wwwroot/myapp.py again
# # Add the following two lines to the file header from gevent import Monkeymonkey.patch_all () from flask import Flaskapp = Flask (__name__) @app. Route ('/') def Hello_world (): Return ' Hello world! ' if __name__ = = ' __main__ ': App.run ()
(3) Start Uwsgi, of course, you can write your own startup script, where the test will not be written
/usr/bin/uwsgi--ini/usr/local/nginx/conf/uwsgi.ini
7. Configure Nginx
(1) Add configuration file cat/usr/local/nginx/conf/vhosts/uwsgi_flask.conf
server {Listen 80;server_name 192.168.1.105;index index.htm index.html;location/{include Uwsgi_params;uwsgi_pass Unix :/tmp/uwsgi.sock;}}
(2) Modify Nginx; add include vhosts/uwsgi_flask.conf;
(3) Start Nginx
Cd/usr/local/nginx/sbin/./nginx
8. Visit http://192.168.1.105
Well, the access is normal, if you want to test flask blocking mode and non-blocking mode, you can add multiple URLs in myapp.py, plus sleep;
Python + flask + uwsgi + gevent + nginx Environment Setup (non-blocking)