Files
video/server/build/lib/app/database.py
T
2026-10-16 13:51:00 +08:00

25 lines
618 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""MySQL 异步连接(SQLAlchemy 2.0 async)"""
from collections.abc import AsyncGenerator
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from app.config import settings
engine = create_async_engine(
settings.database_url,
pool_size=20,
max_overflow=10,
pool_recycle=3600,
pool_pre_ping=True,
echo=False,
)
SessionFactory = async_sessionmaker(
engine, class_=AsyncSession, expire_on_commit=False, autoflush=False
)
async def get_db() -> AsyncGenerator[AsyncSession, None]:
async with SessionFactory() as session:
yield session