设为首页 收藏本站
查看: 1664|回复: 0

[经验分享] flask+gevent+gunicorn+nginx 初试

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-10-13 08:54:35 | 显示全部楼层 |阅读模式
1.安装flask

pip install flask

2.安装gevent

pip install gevent

3.安装gunicorn

pip install gunicorn

版本信息例如以下:

[iyunv@rs-2 ~]# pip list

Flask (0.10.1)

gevent (1.0.1)

greenlet (0.4.2)

gunicorn (18.0)

pip (1.5.5)

setuptools (3.6)

4.安装nginx

下载源代码包安装,版本号信息例如以下

[iyunv@rs-2 sbin]# ./nginx -v

nginx version: nginx/1.7.0

5.nginx配置反向代理

        listen       80;

        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {

            try_files @uri @pp;

        }


        location @pp {

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_set_header Host $http_host;

            proxy_pass http://127.0.0.1:5000;

        }

全部请求转发到gunicorn监听的5000上。

6.gunicorn配置

[iyunv@rs-2 pythonTest]# cat gun.conf

import os

bind='127.0.0.1:5000'

workers=4

backlog=2048

worker_class="gevent" #sync, gevent,meinheld

debug=True

proc_name='gunicorn.pid'

pidfile='/var/log/gunicorn/debug.log'

loglevel='debug'

[iyunv@rs-2 pythonTest]#

7.測试脚本编写

[iyunv@rs-2 pythonTest]# cat run_test.py

from flask import Flask
from flask import render_template_string

import os

from werkzeug.contrib.fixers import ProxyFix

app = Flask(__name__)

@app.route("/")
def index():  
    return "Hello World"


app.wsgi_app = ProxyFix(app.wsgi_app)
if __name__ == "__main__":
    app.run()



8.启动脚本

[iyunv@rs-2 pythonTest]# gunicorn -c gun.conf  run_test:app

看到例如以下部分输入

2014-05-12 10:29:41 [30260] [INFO] Listening at: http://127.0.0.1:5000 (30260)

2014-05-12 10:29:41 [30260] [INFO] Using worker: gevent

2014-05-12 10:29:41 [30265] [INFO] Booting worker with pid: 30265

2014-05-12 10:29:41 [30266] [INFO] Booting worker with pid: 30266

2014-05-12 10:29:41 [30267] [INFO] Booting worker with pid: 30267

2014-05-12 10:29:41 [30268] [INFO] Booting worker with pid: 30268

监听本机的5000port,

工作模式为gevent,

开启4个进程

9.压力測试

在还有一台虚拟上进行用ab模拟并发请求

报错:apr_socket_recv: No route to host (113)

[iyunv@rs-1 ~]# time ab -n 200 -c 200 http://172.16.3.92/

This is ApacheBench, Version 2.3 <$Revision: 1554214 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/


Benchmarking 172.16.3.92 (be patient)

apr_socket_recv: No route to host (113)


real 0m0.078s

user 0m0.021s

sys 0m0.046s

解决方法:关闭目标server的防火墙

[iyunv@rs-2 pythonTest]# service iptables stop

Flushing firewall rules:                                   [  OK ]

Setting chains to policy ACCEPT: filter                    [  OK ]

Unloading iptables modules:                                [  OK ]

ok, 这下能够压力測试了

[iyunv@rs-1 ~]# time ab -n 2000 -c 200 http://172.16.3.92/

This is ApacheBench, Version 2.3 <$Revision: 1554214 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/


Benchmarking 172.16.3.92 (be patient)

Completed 200 requests

Completed 400 requests

Completed 600 requests

Completed 800 requests

Completed 1000 requests

Completed 1200 requests

Completed 1400 requests

Completed 1600 requests

Completed 1800 requests

Completed 2000 requests

Finished 2000 requests



Server Software:        nginx/1.7.0

Server Hostname:        172.16.3.92

Server Port:            80


Document Path:          /

Document Length:        11 bytes


Concurrency Level:      200

Time taken for tests:   16.273 seconds

Complete requests:      2000

Failed requests:        0

Total transferred:      334000 bytes

HTML transferred:       22000 bytes

Requests per second:    122.90 [#/sec] (mean)

Time per request:       1627.313 [ms] (mean)

Time per request:       8.137 [ms] (mean, across all concurrent requests)

Transfer rate:          20.04 [Kbytes/sec] received


Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0   40 486.7      1    8992

Processing:   276 1032 250.5   1028    6300

Waiting:      273 1032 250.5   1027    6300

Total:        283 1072 618.1   1029   15292


Percentage of the requests served within a certain time (ms)

  50%   1029

  66%   1055

  75%   1074

  80%   1096

  90%   1210

  95%   1245

  98%   1361

  99%   2416

100%  15292 (longest request)


real 0m16.316s

user 0m0.598s

sys 0m1.447s

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-25883-1-1.html 上篇帖子: Nginx性能测试工具之http_load 下篇帖子: centos7 nginx前端 apache后端 及多站点简单配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表