7 篇文章带有标签 “gunicorn”

FastAPI 开发 RESTAPI 实践

首页

重定向到 Swagger UI

@app.get("/", include_in_schema=False)
async def index():
    return RedirectResponse('/docs', status_code=303)

使用 route 的 docstring 作为首页内容

@app.get("/", response_class=HTMLResponse, include_in_schema=False)
async def index():
    # 需要过滤的路由
    filted_routes = [
        "/openapi.json",
        "/docs",
        "/docs/oauth2-redirect",
        "/redoc",
        "/static",
        "/"
    ]
    
    routes = []
    for route in app.routes:
        if route.path not in filted_routes:
// ...

Markdown to HTML

安装

pip install markdown

使用

构建容器化 Python 应用程序

这里使用 Ultralytics Serving 作为示例,它是一个基于 FastAPIUltralytics YOLOv8 的模型推理服务。

选择 Python 镜像

Tag Python Version OS Version Size
3.10 3.10 Debian GNU/Linux 11 (bullseye) 861MB
3.10-slim 3.10 Debian GNU/Linux 11 (bullseye) 114MB
3.10-alpine 3.10 Alpine Linux 3.15.0 44.7MB

克隆 Ultralytics Serving

git clone https://github.com/gouchicao/ultralytics-serving.git
cd ultralytics-serving

编写 Dockerfile

采用两阶段构建,第一阶段安装依赖环境和编译应用,第二阶段发布应用。第二阶段可以使用小一点的镜像,比如 python:3.10-slimpython:3.10-alpine

普通版本 FROM python:3.10 AS builder ENV APP_HOME=/ultralytics-serving WORKDIR ${APP_HOME} # 提前安装,因为 cpu 版本需要指定 index-url。

基于 FastAPI 开发 Ultralytics Serving

Ultralytics Serving

Inference service based on Ultralytics

创建虚拟环境

python -m venv venv source venv/bin/activate

安装依赖包

创建 requirements.txt

fastapi
python-multipart
aiofiles
onnxruntime
ultralytics
uvicorn[standard]
gunicorn
pytest
httpx

安装

pip install -r requirements.txt

调试

创建 launch.json 文件,用于调试 FastAPI 应用。

.vscode/launch.json { "version": "0.2.0", "configurations": [ { "name": "Python: FastAPI", "type": "python", "request": "launch", "module": "uvicorn", "args": [ "app.

使用 FastAPI 开发 RESTAPI 服务

创建项目

创建目录

mkdir project
cd project

创建虚拟环境

python -m venv env
source env/bin/activate
# 退出命令 deactivate

创建 requirements.txt 文件

fastapi
python-multipart
aiofiles
uvicorn
gunicorn

安装需要的库

pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

项目目录结构

project
├── app
│   ├── __init__.py
│   ├── dependencies.py
│   ├── main.py
│   └── routers
│       ├── __init__.py
│       ├── files.py
│       └── users.py
└── requirements.txt

main.py import uvicorn from fastapi import FastAPI from .routers import users from .routers import files app = FastAPI(title='REST API Interface', version='1.

FastAPI 上传和下载文件的基准测试

使用 FastAPI 实现了文件的上传和下载,部署服务使用了 uvicorn 和 gunicorn+uvicorn 两种方法。

基准测试工具使用的是 wrk

服务器 CPU 40核,内存 256G,操作系统 Ubuntu 20.04,Python3.9

测试流程

使用的测试图片 health.jpg (256kb)

生成测试数据

生成通过 HTTP POST 发送二进制数据的文件。

python make_http_postdata.py make health.jpg postdata
file: /home/wjj/test/postdata
boundary: gouchicao0123456789

创建用于 wrk 的 lua 脚本:postfile.lua

wrk.method = "POST"
local f = io.open("postdata", "rb")
wrk.body   = f:read("*all")
wrk.headers["Content-Type"] = "multipart/form-data; boundary=gouchicao0123456789"

部署 FastAPI 应用 uvicorn uvicorn app.

阿里云服务器 ECS 开放端口

开放端口设置

配置路径

配置规则

授权策略 优先级 协议类型 端口范围 授权对象 描述
允许 1 自定义 TCP 目的:5000/5000 源:0.0.0.0/0 Flask
允许 1 自定义 TCP 目的:8000/8000 源:0.0.0.0/0 FastAPI

问题

今天通过上面的配置后,发现不能访问,上网搜索了半天,看到有说需要配置 iptables ,于是开始了一顿搜索和学习(Linux上iptables基础应用),进行了如下配置,发现还是访问不了。

iptables -A INPUT -p tcp --dport 5000 -j ACCEPT
iptables -A INPUT -p tcp --dport 8000 -j ACCEPT

解决

突然想到了 Web 应用服务器的配置 --host,我这里用的是 uvicorn,默认使用的 127.0.0.1,这样只能接收本机的访问,需要接收其它主机的访问就需要配置为 0.0.0.0。

Web 应用服务器的配置

  • uvicorn --host 0.0.0.0
  • gunicorn --bind 0.0.0.0

最后测试了一下,发现根本不需要使用 iptables 配置。

参考资料

基于健康码识别的 FastAPI 同步和异步函数的基准测试

健康码识别服务使用了 FastAPI 进行开发的,本周主要工作是为了对健康码识别的服务进行性能调优。接口函数使用了 async 关键字,但是内部的实现并没有使用 await。由于改写成异步代码需要时间,这里并没有改写代码,只是删除了 async 关键字。部署服务使用了 uvicorn 和 gunicorn+uvicorn 两种方法。

基准测试工具使用的是 ab

测试流程

生成测试数据

准备测试图片 health.jpg

echo -n '{"base64": "' > health.json
base64 -w0 health.jpg >> health.json
echo -n '"}' >> health.json

部署服务

uvicorn

docker run --runtime=nvidia --rm -it -e NVIDIA_VISIBLE_DEVICES=2 -p 20001:8000 \
    -v $(pwd):/health_code_service --name=health-uvicorn  health-code-service \
    uvicorn controller:app --host 0.0.0.0 --workers 1
  • workers 并发进程数