当前位置: 首页 > news >正文

上下文管理器 和 contextlib 模块

在 Python 中,上下文管理器(Context Manager) 是一种用于管理资源获取与释放的结构,核心作用是自动化资源的进入和退出(常用于文件操作、数据库连接、线程锁、事务处理等)。它的本质是定义好 enter / exit(同步)或 aenter / aexit(异步) 方法。

1. 基本概念

同步上下文管理器(with)

class MyResource:def __enter__(self):print("获取资源")return selfdef __exit__(self, exc_type, exc_val, exc_tb):print("释放资源")with MyResource() as r:print("处理中")

输出:

获取资源
处理中
释放资源

异步上下文管理器(async with)

适用于异步资源(如:异步数据库、aiofiles、aiohttp 等):

class MyAsyncResource:async def __aenter__(self):print("异步获取资源")return selfasync def __aexit__(self, exc_type, exc_val, exc_tb):print("异步释放资源")async with MyAsyncResource() as r:print("异步处理中")

2. contextlib 模块

contextlib 是 Python 标准库中的一个实用工具模块,专门用于简化上下文管理器(context manager)的创建和使用。

主要功能

  1. 简化上下文管理器的创建 - 无需编写完整的类实现 enterexit 方法
  2. 提供装饰器和工具函数 - 用于常见上下文管理场景
  3. 支持可重用的上下文管理器

核心组件

1. @contextmanager 装饰器

基本用法:

from contextlib import contextmanager@contextmanager
def 管理资源(*args, **kwargs):# 这部分相当于 __enter__ 方法资源 = 获取资源(*args, **kwargs)try:yield 资源  # 在这里返回资源给as变量finally:# 这部分相当于 __exit__ 方法释放资源(资源)# 使用方式
with 管理资源() as 资源:print(资源)

示例:

from contextlib import contextmanager@contextmanager
def open_file(path):f = open(path, 'r')try:yield ffinally:f.close()with open_file("test.txt") as f:print(f.read())

2. @asynccontextmanager(异步)

基本用法:

from contextlib import asynccontextmanager@asynccontextmanager
async def async_resource_manager():# 初始化部分 (相当于 __aenter__)resource = await acquire_resource_async()try:yield resource  # 在这里暂停,将资源提供给 with 块finally:# 清理部分 (相当于 __aexit__)await release_resource_async(resource)# 使用方式
async def main():async with async_resource_manager() as res:await do_something_with(res)

特点:

  • 专为异步代码设计:与普通 @contextmanager 不同,它处理的是异步上下文协议 (aenter/aexit)
  • 必须用 async with 调用:不能与普通 with 语句混用
  • 支持异步清理:finally 块中可以执行 await 操作

示例:

from contextlib import asynccontextmanager
import aiofiles@asynccontextmanager
async def open_file_async(path):f = await aiofiles.open(path, 'r')try:yield ffinally:await f.close()async def run():async with open_file_async("test.txt") as f:content = await f.read()

3. 高级实践:FastAPI 启动生命周期封装

from fastapi import FastAPI
from contextlib import asynccontextmanager
from motor.motor_asyncio import AsyncIOMotorClient@asynccontextmanager
async def lifespan(app: FastAPI):app.state.mongo = AsyncIOMotorClient("mongodb://localhost:27017")["mydb"]yieldapp.state.mongo.client.close()app = FastAPI(lifespan=lifespan)
http://www.lryc.cn/news/587654.html

相关文章:

  • Cocos Creator 高斯模糊效果实现解析
  • 2025高防CDN硬核防御指南:AI+量子加密如何终结DDoS/CC攻击?
  • VyOS起步指南:用Docker快速搭建网络实验环境
  • MCP终极篇!MCP Web Chat项目实战分享
  • android tabLayout 切换fragment fragment生命周期
  • VScode设计平台demo&前端开发中的常见问题
  • CentOS系统哪些版本?分别适用于那些业务或网站类型?
  • VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
  • 【LeetCode】大厂面试算法真题回忆(107)--重组字符串
  • CentOS 7服务器上使用Docker部署Notesnook的详细指导说明
  • CentOS 安装jenkins笔记
  • LVS的集群技术和分布式
  • RabbitMQ中队列长度限制(Queue Length Limit)详解
  • Docker Desktop 挂载本地Win系统配置指南:Redis/MySQL/RabbitMQ持久化与自启设置
  • 【第一章编辑器开发基础第二节编辑器布局_3间距控制(4/4)】
  • RabbitMQ的介绍与安装
  • RabbitMQ的几个模式
  • Redis单线程详解
  • Advanced Database Systems: History of Databases
  • 代数基本定理最简短的证明
  • C++ 中常见的字符串定义方式及其用法
  • 正运动与您相聚2025青岛国际工业自动化技术及装备展览会!
  • 前端字体使用操作
  • YOLOv11调参指南
  • 管程! 解决互斥,同步问题的现代化手段(操作系统os)
  • Linux操作系统从入门到实战(八)详细讲解编译器gcc/g++编译步骤与动静态库链接
  • Rocket常见问题及解决方案
  • H2 与高斯数据库兼容性解决方案:虚拟表与类型处理
  • 第12章:【系统架构设计师】系统架构设计-数据流风格
  • Oracle中的INSTR函数