Python/agent/application/workshop/database.py

31 lines
681 B
Python

# -*- coding: utf-8 -*-
"""
数据库模块
"""
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from asyncpg import Pool, create_pool
# 连接池
connection_pool: Pool | None = None
@asynccontextmanager
async def connection_pool_init() -> AsyncIterator[None]:
"""
连接池初始化
"""
global connection_pool
# 创建连接池
connection_pool = await create_pool(
"postgres://root:PG198752@liubiren.cloud:5201/database",
min_size=1,
max_size=5,
server_settings={"statement_timeout": "5000"},
)
yield
if connection_pool is not None:
await connection_pool.close()