百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

高级 FastAPI 模式:使用 Python 构建可用于生产的 API

itomcoil 2025-03-14 18:07 11 浏览

FastAPI 以其现代的异步优先方法彻底改变了 Python API 开发。我花了大量时间研究 FastAPI,我将分享在生产环境中行之有效的高级模式。

依赖注入

FastAPI 中的依赖注入提供了清晰的关注点分离和高效的资源管理。以下是数据库依赖项的实际实现:

from fastapi import Depends
from sqlalchemy.orm import Session
from contextlib import contextmanager

class Database:
    def __init__(self):
        self.session = None

    @contextmanager
    def get_session(self):
        session = Session()
        try:
            yield session
        finally:
            session.close()

db = Database()

async def get_db():
    with db.get_session() as session:
        yield session

@app.get("/users/{user_id}")
async def get_user(user_id: int, db: Session = Depends(get_db)):
    return db.query(User).filter(User.id == user_id).first()

响应缓存

实现 Redis 缓存可显著提高 API 性能。以下是一个强大的缓存实现:

from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
import pickle

class CustomRedisBackend(RedisBackend):
    async def get(self, key: str):
        value = await self.redis.get(key)
        if value:
            return pickle.loads(value)
        return None

    async def set(self, key: str, value: any, expire: int = None):
        value = pickle.dumps(value)
        await self.redis.set(key, value, expire)

@app.on_event("startup")
async def startup():
    redis = aioredis.Redis(host='localhost', port=6379)
    FastAPICache.init(CustomRedisBackend(redis), prefix="fastapi-cache:")

@app.get("/products/{product_id}")
@FastAPICache.cache(expire=300)
async def get_product(product_id: int):
    return {"product_id": product_id}

后台任务

有效地管理长时间运行的操作至关重要。以下是处理后台任务的模式:

from fastapi import BackgroundTasks
from celery import Celery

celery_app = Celery('tasks', broker='redis://localhost:6379/0')

@celery_app.task
def process_video(video_id: int):
    # Video processing logic
    pass

@app.post("/videos/")
async def upload_video(
    video: UploadFile,
    background_tasks: BackgroundTasks
):
    video_id = save_video(video)
    background_tasks.add_task(process_video.delay, video_id)
    return {"status": "processing"}

速率限制

保护 API 资源需要有效的速率限制。以下是基于 Redis 的实现:

from fastapi import HTTPException
import time

class RateLimiter:
    def __init__(self, redis_client, limit: int, window: int):
        self.redis = redis_client
        self.limit = limit
        self.window = window

    async def is_allowed(self, key: str) -> bool:
        current = int(time.time())
        window_start = current - self.window

        async with self.redis.pipeline() as pipe:
            pipe.zremrangebyscore(key, 0, window_start)
            pipe.zadd(key, {str(current): current})
            pipe.zcard(key)
            pipe.expire(key, self.window)
            results = await pipe.execute()

        return results[2] <= self.limit

@app.get("/api/resource")
async def get_resource(
    redis: Redis = Depends(get_redis),
    user: User = Depends(get_current_user)
):
    rate_limiter = RateLimiter(redis, limit=100, window=3600)
    if not await rate_limiter.is_allowed(f"rate_limit:{user.id}"):
        raise HTTPException(status_code=429, detail="Rate limit exceeded")
    return {"data": "resource"}

自定义中间件

中间件提供了强大的请求/响应修改功能:

from fastapi import Request
from fastapi.middleware.base import BaseHTTPMiddleware
import time

class TimingMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next):
        start_time = time.time()
        response = await call_next(request)
        process_time = time.time() - start_time
        response.headers["X-Process-Time"] = str(process_time)
        return response

app.add_middleware(TimingMiddleware)

API 版本控制

维护 API 版本对于向后兼容性至关重要:

from fastapi import APIRouter

v1_router = APIRouter(prefix="/v1")
v2_router = APIRouter(prefix="/v2")

@v1_router.get("/users/{user_id}")
async def get_user_v1(user_id: int):
    return {"version": "1", "user_id": user_id}

@v2_router.get("/users/{user_id}")
async def get_user_v2(user_id: int):
    return {"version": "2", "user_id": user_id}

app.include_router(v1_router)
app.include_router(v2_router)

错误处理

一致的错误处理提高了 API 的可靠性:

from fastapi import HTTPException
from fastapi.responses import JSONResponse

class CustomException(Exception):
    def __init__(self, message: str, code: str):
        self.message = message
        self.code = code

@app.exception_handler(CustomException)
async def custom_exception_handler(request, exc):
    return JSONResponse(
        status_code=400,
        content={
            "error": {
                "code": exc.code,
                "message": exc.message
            }
        }
    )

@app.get("/items/{item_id}")
async def get_item(item_id: int):
    if item_id < 0:
        raise CustomException(
            message="Invalid item ID",
            code="INVALID_ID"
        )
    return {"item_id": item_id}

测试

全面的测试确保API的可靠性:

from fastapi.testclient import TestClient
import pytest

client = TestClient(app)

@pytest.fixture
def test_db():
    # Setup test database
    db = Database()
    yield db
    # Cleanup

def test_read_item():
    response = client.get("/items/1")
    assert response.status_code == 200
    assert response.json() == {"item_id": 1}

def test_create_item():
    response = client.post(
        "/items/",
        json={"name": "Test Item"}
    )
    assert response.status_code == 201

生产部署

对于生产部署,请考虑以下配置:

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn

app = FastAPI(
    title="Production API",
    description="Production-ready FastAPI application",
    version="1.0.0",
    docs_url="/documentation",
    redoc_url=None
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://allowed-domain.com"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

if __name__ == "__main__":
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        workers=4,
        log_level="info",
        reload=False
    )

这些模式构成了强大的 FastAPI 应用程序的基础。异步功能与适当的资源管理和错误处理相结合,可创建高性能 API。定期测试和监控可确保生产环境中的可靠性。

请记住根据特定要求和规模调整这些模式。FastAPI 的灵活性允许自定义,同时保持性能和代码清晰度。

相关推荐

使用opencv-Python进行图像锐化处理

使用OpenCV函数cv::filter2D执行一些拉普拉斯滤波以进行图像锐化使用OpenCV函数cv::distanceTransform以获得二值图像的派生(derived)表示,...

Python-OpenCV 7. 图像二值化

一、介绍图像二值化(ImageBinarization)就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的黑白效果的过程。在数字图像处理中,二值图像占有非常重要的地位,图...

OpenCV+Python裁剪图像

最近使用OpenCV+Python做了一个程序,功能是自动将照片中的文本部分找出来并裁剪/旋转保存为新的图片。这个功能用专业些的说法就是选择并提取感兴趣区域(ROI(RegionofInteres...

简单易懂的人脸识别!用PythonOpenCV实现(适合初...

前言:OpenCV是一个开源的计算机视觉和机器学习库。它包含成千上万优化过的算法,为各种计算机视觉应用提供了一个通用工具包。根据这个项目的关于页面,OpenCV已被广泛运用在各种项目上,从谷歌街景...

OpenCV行人检测应用方案--基于米尔全志T527开发板

本文将介绍基于米尔电子MYD-LT527开发板(米尔基于全志T527开发板)的OpenCV行人检测方案测试。摘自优秀创作者-小火苗一、软件环境安装1.在全志T527开发板安装OpenCVsudoap...

纯Python构建Web应用:Remi与 OpenCV 结合实现图像处理与展示

引言大家好,我是ICodeWR。在前几篇文章中,我们介绍了Remi的基础功能、多页面应用、动态更新、与Flask结合、与数据库结合、与Matplotlib结合以及与Pandas结合。...

【AI实战项目】基于OpenCV的“颜色识别项目”完整操作过程

OpenCV是一个广受欢迎且极为流行的计算机视觉库,它因其强大的功能、灵活性和开源特性而在开发者和研究者中备受青睐。学习OpenCV主要就是学习里面的计算机视觉算法。要学习这些算法的原理,知道它们适用...

Python自动化操控术:PyAutoGUI全场景实战指南

一、PyAutoGUI核心武器库解析1.1鼠标操控三剑客importpyautogui#绝对坐标移动(闪电速度)pyautogui.moveTo(100,200,duration=0....

从零开始学python爬虫(七):selenium自动化测试框架的介绍

本节主要学习selenium自动化测试框架在爬虫中的应用,selenium能够大幅降低爬虫的编写难度,但是也同样会大幅降低爬虫的爬取速度。在逼不得已的情况下我们可以使用selenium进行爬虫的编写。...

「干货分享」推荐5个可以让你事半功倍的Python自动化脚本

作者:俊欣来源:关于数据分析与可视化相信大家都听说自动化流水线、自动化办公等专业术语,在尽量少的人工干预的情况下,机器就可以根据固定的程序指令来完成任务,大大提高了工作效率。今天小编来为大家介绍几个P...

python+selenium+pytesseract识别图片验证码

一、selenium截取验证码#私信小编01即可获取大量Python学习资源#私信小编01即可获取大量Python学习资源#私信小编01即可获取大量Python学习资源importjso...

Python爬虫实战 | 利用多线程爬取 LOL 高清壁纸

一、背景介绍随着移动端的普及出现了很多的移动APP,应用软件也随之流行起来。最近看到英雄联盟的手游上线了,感觉还行,PC端英雄联盟可谓是爆火的游戏,不知道移动端的英雄联盟前途如何,那今天我们使用到...

一套真实的Python面试题,几十个题目汇总

1.(1)python下多线程的限制以及多进程中传递参数的方式python多线程有个全局解释器锁(globalinterpreterlock),这个锁的意思是任一时间只能有一个线程使用解释器,跟...

一文读透,Python暴力(BF)字符串匹配算法到 KMP 算法之间的变化

1.字符串匹配算法所谓字符串匹配算法,简单地说就是在一个目标字符串中查找是否存在另一个模式字符串。如在字符串"ABCDEFG"中查找是否存在“EF”字符串。可以把字符...

Python实现屏幕自动截图

教程目录需要实现的功能:自动屏幕截图具体需求:1.支持设置截图频率和截图文件存储路径2.在存储截图时判断与前一张截图的相似度,只有屏幕发生了显著的变化才存储截图所需技术(搜索关键词):1.屏幕截...