flask+gevent+gunicorn+nginx 初試

1.安裝flaskpython

pip install flasknginx

2.安裝geventapache

pip install geventflask

3.安裝gunicorn併發

pip install gunicornapp

版本信息例如如下:socket

[root@rs-2 ~]# pip listspa

Flask (0.10.1).net

gevent (1.0.1)debug

greenlet (0.4.2)

gunicorn (18.0)

pip (1.5.5)

setuptools (3.6)


4.安裝nginx

下載源代碼包安裝,版本號信息例如如下

[root@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配置

[root@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'

[root@rs-2 pythonTest]# 


7.測試腳本編寫

[root@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.啓動腳本

[root@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)

[root@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的防火牆

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

Flushing firewall rules:                                   [  OK ]

Setting chains to policy ACCEPT: filter                    [  OK ]

Unloading iptables modules:                                [  OK ]


ok, 這下可以壓力測試了

[root@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

sys0m1.447s

相關文章
相關標籤/搜索