编程语言
首页 > 编程语言> > Python Flask Web 高并发实现

Python Flask Web 高并发实现

作者:互联网

Flask超高并发,Flask多进程gevent ,Flask非gunicorn gevent多进程

1. flask + gevent + multiprocess + wsgi简介

常常大家都是用gunicorn来解决flask后端部署并发的问题, 然而觉得自启多进程是为更优雅的高并发方式。这样就不需要gunicorn了。也没有额外的第三方部署工作,于是有了以下flask + gevent + multiprocess + wsgi的测试

2. flask + gevent + multiprocess + wsgi程序代码app.py

# coding: utf-8
# flask + gevent + multiprocess + wsgi

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

import datetime
import os
from multiprocessing import cpu_count, Process
from flask import Flask, jsonify


app = Flask(__name__)

@app.route("/cppla", methods=['GET'])
def function_benchmark():
    return jsonify(
        {
            "status": "ok",
            "time": datetime.datetime.now().strftime('%Y-%m-%d %H:%M'),
            "pid": os.getpid()
        }
    ), 200

def run(MULTI_PROCESS):
    if MULTI_PROCESS == False:
        WSGIServer(('0.0.0.0', 8080), app).serve_forever()
    else:
        mulserver = WSGIServer(('0.0.0.0', 8080), app)
        mulserver.start()

        def server_forever():
            mulserver.start_accepting()
            mulserver._stop_event.wait()

        for i in range(cpu_count()):
            p = Process(target=server_forever)
            p.start()

if __name__ == "__main__":
    # 单进程 + 协程
    run(False)
    # 多进程 + 协程
    # run(True)

3. 测试环境

	机器配置
		4vCPU
		8GB
	环境配置
		Centos 8
		Python 3.6.8
		Flask  1.1.2
		Gevent 1.5.0
	压测工具
		WRK
		4H8G机器

4. 单进程 + 协程 + WSGI

4.1 测试结果

QPS : 2361 r/s

4.2 测试详情

[root@vm5 ~]# nohup python3 cppla.py &
[1] 9371
[root@vm5 ~]# nohup: ignoring input and appending output to 'nohup.out'
 
[root@vm5 ~]# ps -ef | grep cppla
root      9371  4184  4 03:33 pts/0    00:00:00 python3 cppla.py
root      9377  4184  0 03:33 pts/0    00:00:00 grep --color=auto cppla
[root@vm5 ~]#
[root@vm4 wrk]# curl 10.10.10.5:8080/cppla
{"pid":9371,"status":"ok","time":"2020-04-20 04:19"}
[root@vm4 wrk]# curl 10.10.10.5:8080/cppla
{"pid":9371,"status":"ok","time":"2020-04-20 04:19"}
[root@vm4 wrk]# curl 10.10.10.5:8080/cppla
{"pid":9371,"status":"ok","time":"2020-04-20 04:19"}
[root@vm4 wrk]# wrk -t12 -c400 -d30s http://10.10.10.5:8080/cppla
Running 30s test @ http://10.10.10.5:8080/cppla
  12 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   164.40ms   21.15ms 515.30ms   80.85%
    Req/Sec   199.61     47.81   565.00     70.38%
  71237 requests in 30.10s, 10.95MB read
Requests/sec:   2366.72
Transfer/sec:    372.44KB
[root@vm4 wrk]# wrk -t20 -c800 -d30s http://10.10.10.5:8080/cppla   
Running 30s test @ http://10.10.10.5:8080/cppla
  20 threads and 800 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   329.39ms   93.86ms   1.78s    92.80%
    Req/Sec   126.19     80.69   696.00     67.71%
  71075 requests in 30.10s, 10.92MB read
  Socket errors: connect 0, read 0, write 0, timeout 97
Requests/sec:   2361.39
Transfer/sec:    371.65KB
[root@vm4 wrk]#

5. 多进程 + 协程 + WSGI

5.1 测试结果

QPS : 7500 r/s

5.2 测试详情

[root@vm5 ~]# nohup python3 cppla.py &
[1] 9537
[root@vm5 ~]# nohup: ignoring input and appending output to 'nohup.out'

[root@vm5 ~]# ps -ef | grep cppla
root 9537 4184 5 04:32 pts/0 00:00:00 python3 cppla.py
root 9542 9537 0 04:32 pts/0 00:00:00 python3 cppla.py
root 9543 9537 0 04:32 pts/0 00:00:00 python3 cppla.py
root 9544 9537 0 04:32 pts/0 00:00:00 python3 cppla.py
root 9545 9537 0 04:32 pts/0 00:00:00 python3 cppla.py
root 9547 4184 0 04:32 pts/0 00:00:00 grep --color=auto cppla
[root@vm5 ~]#
[root@vm4 wrk]# curl 10.10.10.5:8080/cppla
{"pid":9543,"status":"ok","time":"2020-04-20 04:34"}
[root@vm4 wrk]# curl 10.10.10.5:8080/cppla
{"pid":9542,"status":"ok","time":"2020-04-20 04:34"}
[root@vm4 wrk]# curl 10.10.10.5:8080/cppla
{"pid":9545,"status":"ok","time":"2020-04-20 04:34"}
[root@vm4 wrk]# wrk -t12 -c400 -d30s http://10.10.10.5:8080/cppla
Running 30s test @ http://10.10.10.5:8080/cppla
  12 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    56.10ms   15.16ms 187.30ms   85.05%
    Req/Sec   590.77     79.95   830.00     67.97%
  212138 requests in 30.08s, 32.60MB read
Requests/sec:   7051.89
Transfer/sec:      1.08MB
[root@vm4 wrk]# wrk -t20 -c800 -d30s http://10.10.10.5:8080/cppla
Running 30s test @ http://10.10.10.5:8080/cppla
  20 threads and 800 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   101.59ms   40.23ms 337.80ms   66.06%
    Req/Sec   394.20    109.48     0.97k    74.47%
  235844 requests in 30.10s, 36.25MB read
Requests/sec:   7835.77
Transfer/sec:      1.20MB
[root@vm4 wrk]#

补充:4核8线程3.6GHZ物理机上多进程并发高达15000QPS

也就是“理想”情况,每秒1w5 QPS,每小时5400万请求。

[root@vm4 wrk]# wrk -t30 -c1000 -d30s http://10.10.10.1:8080/cppla 
Running 30s test @ http://10.10.10.1:8080/cppla
  30 threads and 1000 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    67.67ms   18.54ms 312.76ms   81.81%
    Req/Sec   489.92     76.58   710.00     68.65%
  440105 requests in 30.09s, 68.07MB read
Requests/sec:  14626.25
Transfer/sec:      2.26MB
[root@vm4 wrk]#

参考链接:

1. https://www.cplusplus.me/2984.html

标签:Web,00,8080,04,Python,wrk,Flask,cppla,root
来源: https://blog.csdn.net/qq_38236744/article/details/119252923