在构建现代 Web API 服务时,Python 开发者的首选已经从传统的 Flask 转向更快、更现代化的框架——FastAPI。它基于 Starlette + Pydantic 构建,具备异步支持、自动文档生成、极高性能等优势。本文将从入门到部署,手把手带你搭建一个高性能 API 服务。
一、FastAPI 的优势
-
✅ 异步支持(基于
asyncio
) -
✅ 自动 OpenAPI 文档 (
/docs
) -
✅ 请求参数自动验证(Pydantic)
-
✅ 性能媲美 Node.js / Go(Uvicorn 驱动)
二、安装 FastAPI 和 Uvicorn
bash
复制编辑
pip install fastapi uvicorn
三、Hello FastAPI 示例
📄 main.py
python
复制编辑
from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello, FastAPI!"}
运行:
bash
复制编辑
uvicorn main:app --reload
浏览器访问:http://127.0.0.1:8000/
自动文档:http://127.0.0.1:8000/docs
四、定义接口参数和模型
python
复制编辑
from pydantic import BaseModel class User(BaseModel): username: str email: str age: int @app.post("/users/") async def create_user(user: User): return {"status": "success", "data": user}
POST /users/
请求:
json
复制编辑
{ "username": "花花", "email": "hua@example.com", "age": 28 }
五、路径参数与查询参数
python
复制编辑
@app.get("/items/{item_id}") async def read_item(item_id: int, q: str = None): return {"item_id": item_id, "query": q}
访问示例:http://localhost:8000/items/1?q=abc
六、异步数据库操作(SQLModel 示例)
bash
复制编辑
pip install sqlmodel[sqlite]
python
复制编辑
from sqlmodel import Field, SQLModel, create_engine, Session class Blog(SQLModel, table=True): id: int | None = Field(default=None, primary_key=True) title: str content: str engine = create_engine("sqlite:///db.sqlite") SQLModel.metadata.create_all(engine) @app.post("/blogs/") async def create_blog(blog: Blog): with Session(engine) as session: session.add(blog) session.commit() session.refresh(blog) return blog
七、部署 FastAPI 到服务器(Gunicorn + Uvicorn)
-
安装依赖:
bash
复制编辑
pip install gunicorn uvicorn
-
生产运行:
bash
复制编辑
gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app
-
使用 Nginx 做反向代理:
nginx
复制编辑
server { listen 80; server_name api.myserver.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
-
启动并守护进程:
bash
复制编辑
nohup gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app &
八、性能测试(与 Flask 对比)
使用 wrk
或 ab
进行压力测试:
-
Flask(单线程):约 800 req/s
-
FastAPI(async + 4 workers):约 3500+ req/s
九、常用插件推荐
插件 | 用途 |
---|---|
fastapi-jwt-auth | JWT 权限控制 |
fastapi-mail | 邮件发送 |
fastapi-users | 快速用户系统 |
SQLModel / TortoiseORM | 异步 ORM 支持 |
十、总结
FastAPI 是当下 Python 构建 Web API 的首选框架。它在性能、开发体验和生态上都十分优秀,特别适合用来搭建:
-
微服务后端接口
-
高性能数据服务
-
WebSocket 服务
-
第三方 API 接入网关
https://bigu.wang
https://www.bigu.wang
https://binm.wang
https://www.binm.wang
https://bint.wang
https://www.bint.wang
https://biop.wang
https://www.biop.wang
https://bits.wang
https://www.bits.wang
https://bjqb.wang
https://www.bjqb.wang
https://bjsm.wang
https://www.bjsm.wang
https://bleo.wang
https://www.bleo.wang
https://ono.wang
https://www.ono.wang
https://onz.wang
https://www.onz.wang
https://opo.wang
https://www.opo.wang
https://osm.wang
https://www.osm.wang
https://osn.wang
https://www.osn.wang
https://ovi.wang
https://www.ovi.wang
https://oxq.wang
https://www.oxq.wang
https://oti.wang
https://www.oti.wang
https://owu.wang
https://www.owu.wang
https://piq.wang
https://www.piq.wang
https://qmi.wang
https://www.qmi.wang
https://qki.wang
https://www.qki.wang
https://ref.wang
https://www.ref.wang
https://sak.wang
https://www.sak.wang
https://sar.wang
https://www.sar.wang
https://sfa.wang
https://www.sfa.wang
https://sfe.wang
https://www.sfe.wang
https://sgo.wang
https://www.sgo.wang
https://sku.wang
https://www.sku.wang
https://ycxjz.cn
https://www.ycxjz.cn
https://bnbmhomes.cn
https://www.bnbmhomes.cn
https://jinjianzuche.com
https://www.jinjianzuche.com
https://ahswt.cn
https://www.ahswt.cn
https://szwandaj.cn
https://www.szwandaj.cn
https://psbest.cn
https://www.psbest.cn
https://shanghai-arnold.cn
https://www.shanghai-arnold.cn
https://zgsscw.com
https://www.zgsscw.com
https://shxqth.cn
https://www.shxqth.cn
https://wdxj.cn
https://www.wdxj.cn
https://jad168.com
https://www.jad168.com
https://ultratrailms.cn
https://www.ultratrailms.cn
https://tztsjd.cn
https://www.tztsjd.cn
https://csqcbx.cn
https://www.csqcbx.cn
https://qazit.cn
https://www.qazit.cn
https://ahzjyl.cn
https://www.ahzjyl.cn