Flask是Python中一个微型的Web开发框架。在debug 模式 或 单纯的 uwsgi模式下,flask是阻塞模式的,也就是说一次只能效应一个请求,或者在uwsgi 开启多进程,响应已知的请求个数;我们这里使用 uwsgi 和 gevent 配合nginx 解决flask的阻塞模式。
原文链接:http://www.huangdc.com/96
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 版本
[root@localhost ~]# python -V
Python 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.gz
cd pip-7.1.0
python setup.py install
## 提示下列信息,,且无报错,说明安装成功
Installed /usr/lib/python2.7/site-packages/pip-7.1.0-py2.7.egg
Processing dependencies for pip==7.1.0
Finished 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.1
python setup.py install
## 提示下列信息,,且无报错,说明安装成功
Installed /usr/lib/python2.7/site-packages/setuptools-18.1-py2.7.egg
Processing dependencies for setuptools==18.1
Finished 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.gz
Installing collected packages: uwsgi
Running setup.py install for uwsgi
Successfully installed uwsgi-2.0.11.1
安装gevent
pip install gevent
## 提示下列信息,,且无报错,说明安装成功
Running setup.py install for greenlet
Running setup.py install for gevent
Successfully installed gevent-1.0.2 greenlet-0.4.7
下载 并 编译安装nginx
useradd www
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar 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_module
make
make install
5、 新建flask app 文件 /data/wwwroot/myapp.py:
[root@localhost ~]# mkdir /data/wwwroot -p
[root@localhost ~]# cd /data/wwwroot/
[root@localhost wwwroot]# cat myapp.py
from flask import Flask
app = 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 monkey
monkey.patch_all()
from flask import Flask
app = 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 ;访问测试一下即可
原文链接:http://www.huangdc.com/96
来源:oschina
链接:https://my.oschina.net/u/588586/blog/489717