python flask同步/异步性能对比

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python flask同步/异步性能对比相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
Author      : Hu Wenchao
Description : flask 同步/异步性能对比

# 同步方式启动app:
$ python app.py sync

# 异步方式启动app:
$ python app.py

# 用ab做测试
$ ab -n 10 -c 10 http://127.0.0.1:4000/

执行结果详情见下,从中可以看出,执行10个请求,同步方式耗时11s+,异步方式耗时2s+,
系统吞吐量大大增加


同步执行结果:
$ ab -n 10 -c 10 http://127.0.0.1:4000/
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient).....done


Server Software:        Werkzeug/0.11.9
Server Hostname:        127.0.0.1
Server Port:            4000

Document Path:          /
Document Length:        249 bytes

Concurrency Level:      10
Time taken for tests:   11.190 seconds
Complete requests:      10
Failed requests:        0
Total transferred:      4050 bytes
HTML transferred:       2490 bytes
Requests per second:    0.89 [#/sec] (mean)
Time per request:       11189.965 [ms] (mean)
Time per request:       1118.996 [ms] (mean, across all concurrent requests)
Transfer rate:          0.35 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:  1198 5956 3322.3   6353   11189
Waiting:     1198 5955 3322.3   6353   11189
Total:       1199 5956 3322.2   6354   11189

Percentage of the requests served within a certain time (ms)
  50%   6354
  66%   7410
  75%   8439
  80%   9977
  90%  11189
  95%  11189
  98%  11189
  99%  11189
 100%  11189 (longest request)

异步执行结果:
$ ab -n 10 -c 10 http://127.0.0.1:4000/
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient).....done


Server Software:
Server Hostname:        127.0.0.1
Server Port:            4000

Document Path:          /
Document Length:        60076 bytes

Concurrency Level:      10
Time taken for tests:   2.207 seconds
Complete requests:      10
Failed requests:        9
   (Connect: 0, Receive: 0, Length: 9, Exceptions: 0)
Total transferred:      577392 bytes
HTML transferred:       576012 bytes
Requests per second:    4.53 [#/sec] (mean)
Time per request:       2206.676 [ms] (mean)
Time per request:       220.668 [ms] (mean, across all concurrent requests)
Transfer rate:          255.52 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   1.6      3       5
Processing:  1628 1816 154.7   1770    2202
Waiting:     1628 1816 154.7   1770    2202
Total:       1633 1819 155.0   1774    2206

Percentage of the requests served within a certain time (ms)
  50%   1774
  66%   1796
  75%   1811
  80%   1937
  90%   2206
  95%   2206
  98%   2206
  99%   2206
 100%   2206 (longest request)


'''
from gevent import monkey
from gevent.pywsgi import WSGIServer
monkey.patch_all()

from werkzeug.contrib.fixers import ProxyFix


from flask import Flask
import requests
app = Flask(__name__)


@app.route('/')
def block():
    return requests.get('https://api.github.com/events').text


def async():
    "Start gevent WSGI server"
    # use gevent WSGI server instead of the Flask
    http = WSGIServer(('', 4000), app.wsgi_app)
    # TODO gracefully handle shutdown
    http.serve_forever()


if __name__ == "__main__":
    import sys
    if len(sys.argv) > 1 and sys.argv[1] == 'sync':
        # 1. 用wsgi方式启动
        app.wsgi_app = ProxyFix(app.wsgi_app)
        app.run(host='0.0.0.0', port=4000)
    else:
        # 2. 用 gevent 异步方式启动
        async()

以上是关于python flask同步/异步性能对比的主要内容,如果未能解决你的问题,请参考以下文章

Flask入门邮件同步与异步发送

Python Flask后端异步处理

python 同步与异步性能区别

java io读取性能对比

flask_day01

Flask 与 Django 框架对比