from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession from sqlalchemy.orm import sessionmaker from config import postgres_settings # 创建异步数据库引擎 engine = create_async_engine( postgres_settings.url, echo=False, future=True ) # 创建异步会话工厂 AsyncSessionLocal = sessionmaker( engine, class_=AsyncSession, expire_on_commit=False ) async def get_db(): """获取数据库会话""" async with AsyncSessionLocal() as session: try: yield session finally: await session.close()