提问人:Samich80 提问时间:11/5/2023 最后编辑:Samich80 更新时间:11/5/2023 访问量:29
LifetimeController中间件模拟从 aiogram2 到 aiogram3
LifetimeControllerMiddleware analog from aiogram2 to aiogram3
问:
在 aiogram2 中,我有代码:
class DatabaseMiddleware(LifetimeControllerMiddleware):
def __init__(self, async_session_maker):
super().__init__()
self.async_session_maker = async_session_maker
async def pre_process(self, obj, data, *args):
session: AsyncSession = self.async_session_maker()
data["session"] = session
data["repo"] = Repo(session=session)
async def post_process(self, obj, data, *args):
del data["repo"]
session: AsyncSession = data.get("session")
await session.close()
但是 aiogram3 中没有 LifetimeControllerMiddleware,aiogram3 中如何替换 LifetimeControllerMiddleware?
class DatabaseMiddleware(BaseMiddleware):
def __init__(self, async_session_maker):
super().__init__()
self.async_session_maker = async_session_maker
async def __call__(
self,
handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]],
event: TelegramObject,
data: Dict[str, Any]
) -> Any:
session: AsyncSession = self.async_session_maker()
data["session"] = session
data["repo"] = Repo(session=session)
return await handler(event, data)
错误:
SAWarning:垃圾回收器正在尝试清理0x000001ACE7450C80>>处的未签入连接 <AdaptedConnection <asyncpg.connection.Connection 对象,该对象将被终止。请确保通过调用或使用适当的上下文管理器来管理其生命周期,将 SQLAlchemy 池连接显式返回到池中。
架构 = self._schema_type_to_method[schema['type']](schema.copy(), f)
错误:sqlalchemy.pool.impl.QueuePool:垃圾回收器正在尝试清理0x000001ACE613F060>>处的未签入连接 <AdaptedConnection <asyncpg.connection.Connection 对象,该对象将被终止。请确保通过调用或使用适当的上下文管理器来管理其生命周期,将 SQLAlchemy 池连接显式返回到池中。close()
close()
答: 暂无答案
评论