[python]使用gunicorn部署fastapi服务

前言

Gunicorn是一种流行的WSGI HTTP服务器,常用于部署Django和Flask等Python Web框架程序。Gunicorn具有轻量级、高稳定性和高性能等特性,可以轻易提高Python WSGI App运行时的性能。

基本原理

Gunicorn采用了pre-fork模型,也就是一个工作进程和多个worker进程的工作模式。在这个模型中,master进程负责接收并处理外部的连接请求,并将这些请求分配给多个worker进程来处理。

当一个gunicorn服务启动时,master进程会首先创建多个worker工作进程,并将它们初始化为一个无限循环的状态,确保worker进程可以不断地等待接收新请求。当一个请求进来,master会将该请求分发给其中一个worker进程。不同的请求分发到不同的worker进程,可以有效提高请求的响应速度和并发处理能力。当某个worker进程崩溃或异常终止时,master进程会自动重新启动一个新的worker工作进程。

安装

pip install gunicorn

示例

首先编写一个简单的服务,请求响应一个字符串"ok",代码文件名为demo1.py

from fastapi import FastAPI
import uvicorn
from fastapi.responses import PlainTextResponse

app = FastAPI()

@app.get("/")
async def root():
    return PlainTextResponse("ok")

if __name__ == "__main__":
    uvicorn.run(app=app, host="0.0.0.0", port=8000, access_log=False)

正常启动,然后使用wrk测试。使用uvicorn的qps为11733.60

$ ps -ef | grep demo1
atlas       2449    1162 44 22:06 pts/0    00:01:11 python demo1.py
atlas       2478    2462  0 22:08 pts/6    00:00:00 grep demo1

$ wrk -t2 -c 100 -d 60s http://127.0.0.1:8000
Running 1m test @ http://127.0.0.1:8000
  2 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     8.59ms    4.36ms 276.86ms   91.44%
    Req/Sec     5.90k   520.55     7.84k    68.54%
  704720 requests in 1.00m, 90.73MB read
Requests/sec:  11733.60
Transfer/sec:      1.51MB

使用gunicorn启动。wrk的测试结果:QPS为25859.47,性能直接提升120%

$ gunicorn demo1:app -b 127.0.0.1:8000 -w 4 -k uvicorn.workers.UvicornWorker

$ ps -ef | grep demo1
atlas       2640    1162  0 22:12 pts/0    00:00:00 /home/atlas/workspace/fastapi-study/bin/python3 /home/atlas/workspace/fastapi-study/bin/gunicorn demo1:app -b 127.0.0.1:8000 -w 4 -k uvicorn.workers.UvicornWorker
atlas       2641    2640  8 22:12 pts/0    00:00:40 /home/atlas/workspace/fastapi-study/bin/python3 /home/atlas/workspace/fastapi-study/bin/gunicorn demo1:app -b 127.0.0.1:8000 -w 4 -k uvicorn.workers.UvicornWorker
atlas       2642    2640  8 22:12 pts/0    00:00:40 /home/atlas/workspace/fastapi-study/bin/python3 /home/atlas/workspace/fastapi-study/bin/gunicorn demo1:app -b 127.0.0.1:8000 -w 4 -k uvicorn.workers.UvicornWorker
atlas       2645    2640 10 22:12 pts/0    00:00:47 /home/atlas/workspace/fastapi-study/bin/python3 /home/atlas/workspace/fastapi-study/bin/gunicorn demo1:app -b 127.0.0.1:8000 -w 4 -k uvicorn.workers.UvicornWorker
atlas       2646    2640 11 22:12 pts/0    00:00:52 /home/atlas/workspace/fastapi-study/bin/python3 /home/atlas/workspace/fastapi-study/bin/gunicorn demo1:app -b 127.0.0.1:8000 -w 4 -k uvicorn.workers.UvicornWorker
atlas       2665    2462  0 22:20 pts/6    00:00:00 grep demo1

$ wrk -t2 -c 100 -d 60s http://127.0.0.1:8000
Running 1m test @ http://127.0.0.1:8000
  2 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     4.13ms    3.38ms 112.09ms   88.85%
    Req/Sec    13.00k     1.44k   19.86k    71.58%
  1554274 requests in 1.00m, 200.11MB read
Requests/sec:  25859.47
Transfer/sec:      3.33MB

热门相关:超级吸收   腹黑大神:捡个萌宠带回家   黄金渔场   以权谋妻   火力法则