標籤:
Flask是Python中一個微型的Web開發架構。在debug 模式 或 單純的 uwsgi模式下,flask是阻塞模式的,也就是說一次只能效應一個請求,或者在uwsgi 開啟多進程,響應已知的請求個數;我們這裡使用 uwsgi 和 gevent 配合nginx 解決flask的阻塞模式。
1、環境
CentOS Linux release 7.0.1406 (Core)
Python 2.7.5
2、安裝類庫
yum install python-devel zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc
3、下載並安裝python 包管理軟體 pip
查看python 版本
[[email protected] ~]# python -VPython 2.7.5
下載 pip-7.1.0.tar.gz
wget https://pypi.python.org/packages/source/p/pip/pip-7.1.0.tar.gz#md5=d935ee9146074b1d3f26c5f0acfd120e
解壓安裝
tar zxf pip-7.1.0.tar.gzcd pip-7.1.0python setup.py install## 提示下列資訊,,且無報錯,說明安裝成功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
下載 setuptools-18.1.tar.gz
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-18.1.tar.gz#md5=f72e87f34fbf07f299f6cb46256a0b06
解壓安裝
tar zxf setuptools-18.1.tar.gz cd setuptools-18.1python setup.py install## 提示下列資訊,,且無報錯,說明安裝成功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、安裝 flask / uwsgi / gevent / nignx
安裝 flask
pip install flask## 提示下列資訊,,且無報錯,說明安裝成功Successfully installed Jinja2-2.8 MarkupSafe-0.23 Werkzeug-0.10.4 flask-0.10.1 itsdangerous-0.24
安裝uwsgi
pip install uwsgi## 提示下列資訊,,且無報錯,說明安裝成功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
安裝gevent
pip install gevent## 提示下列資訊,,且無報錯,說明安裝成功 Running setup.py install for greenlet Running setup.py install for geventSuccessfully installed gevent-1.0.2 greenlet-0.4.7
下載 並 編譯安裝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、 建立flask app 檔案 /data/wwwroot/myapp.py:
[[email protected] ~]# mkdir /data/wwwroot -p[[email protected] ~]# cd /data/wwwroot/[[email protected] wwwroot]# cat myapp.pyfrom flask import Flaskapp = Flask(__name__)@app.route(‘/‘)def hello_world(): return ‘Hello World!‘if __name__ == ‘__main__‘: app.run()
6、配置 uWSGI 及 gevent
(1)配置 uwsgi.ini 檔案 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 目錄 module = myapp ## flask app 名稱,也就是我們的 myapp.py callable = app touch-reload = /data/wwwroot/ pidfile = /var/run/uwsgi.pid daemonize = /usr/local/nginx/logs/uwsgi.log gevent = 100 ## 加入 gevent = 100 ,非阻塞模式
(2) 再次配置 cat /data/wwwroot/myapp.py
## 在檔案頭加入下面兩行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)啟動 uwsgi ,當然你可以自己寫個啟動指令碼,這裡測試就不寫了
/usr/bin/uwsgi --ini /usr/local/nginx/conf/uwsgi.ini
7、配置nginx
(1)添加設定檔 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)修改nginx ;加入include vhosts/uwsgi_flask.conf;
(3)啟動nginx
cd /usr/local/nginx/sbin/./nginx
8、訪問 http://192.168.1.105
好了,訪問正常,如果想測試flask的阻塞模式和非阻塞模式的話,你可以在 myapp.py 添加多個url ,並且加上sleep ;訪問測試一下即可
python + flask + uwsgi + gevent + nginx 環境搭建(非阻塞)