[python]使用gunicorn部署fastapi服务

简介: [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
相关文章
FastAPI与Selenium:打造高效的Web数据抓取服务 —— 采集Pixabay中的图片及相关信息
本文介绍了如何使用FastAPI和Selenium搭建RESTful接口,访问免版权图片网站Pixabay并采集图片及其描述信息。通过配置代理IP、User-Agent和Cookie,提高爬虫的稳定性和防封禁能力。环境依赖包括FastAPI、Uvicorn和Selenium等库。代码示例展示了完整的实现过程,涵盖代理设置、浏览器模拟及数据提取,并提供了详细的中文注释。适用于需要高效、稳定的Web数据抓取服务的开发者。
62 15
FastAPI与Selenium:打造高效的Web数据抓取服务 —— 采集Pixabay中的图片及相关信息
FastAPI + ONNX 部署机器学习模型最佳实践
本文介绍了如何结合FastAPI和ONNX实现机器学习模型的高效部署。面对模型兼容性、性能瓶颈、服务稳定性和安全性等挑战,FastAPI与ONNX提供了高性能、易于开发维护、跨框架支持和活跃社区的优势。通过将模型转换为ONNX格式、构建FastAPI应用、进行性能优化及考虑安全性,可以简化部署流程,提升推理性能,确保服务的可靠性与安全性。最后,以手写数字识别模型为例,展示了完整的部署过程,帮助读者更好地理解和应用这些技术。
103 20
Python Web 框架 FastAPI
FastAPI 是一个现代的 Python Web 框架,专为快速构建 API 和在线应用而设计。它凭借速度、简单性和开发人员友好的特性迅速走红。FastAPI 支持自动文档生成、类型提示、数据验证、异步操作和依赖注入等功能,极大提升了开发效率并减少了错误。安装简单,使用 pip 安装 FastAPI 和 uvicorn 即可开始开发。其优点包括高性能、自动数据验证和身份验证支持,但也存在学习曲线和社区资源相对较少的缺点。
94 15
如何使用阿里云的语音合成服务(TTS)将文本转换为语音?本文详细介绍了从注册账号、获取密钥到编写Python代码调用TTS服务的全过程
如何使用阿里云的语音合成服务(TTS)将文本转换为语音?本文详细介绍了从注册账号、获取密钥到编写Python代码调用TTS服务的全过程。通过简单的代码示例,展示如何将文本转换为自然流畅的语音,适用于有声阅读、智能客服等场景。
1121 3
将 Python UDF 部署到 Apache IoTDB 的详细步骤与注意事项
【10月更文挑战第21天】将 Python UDF 部署到 Apache IoTDB 中需要一系列的步骤和注意事项。通过仔细的准备、正确的部署和测试,你可以成功地将自定义的 Python UDF 应用到 Apache IoTDB 中,为数据处理和分析提供更灵活和强大的支持。在实际操作过程中,要根据具体情况进行调整和优化,以确保实现最佳的效果。还可以结合具体的代码示例和实际部署经验,进一步深入了解和掌握这一过程。
48 2
【Azure Bot Service】部署Python ChatBot代码到App Service中
本文介绍了使用Python编写的ChatBot在部署到Azure App Service时遇到的问题及解决方案。主要问题是应用启动失败,错误信息为“Failed to find attribute 'app' in 'app'”。解决步骤包括:1) 修改`app.py`文件,添加`init_func`函数;2) 配置`config.py`,添加与Azure Bot Service认证相关的配置项;3) 设置App Service的启动命令为`python3 -m aiohttp.web -H 0.0.0.0 -P 8000 app:init_func`。
【Azure Function】Python Function部署到Azure后报错No module named '_cffi_backend'
ERROR: Error: No module named '_cffi_backend', Cannot find module. Please check the requirements.txt file for the missing module.
114 2

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等