import{_ as i,c as a,a7 as n,o as t}from"./chunks/framework.jrU0lkHV.js";const d=JSON.parse('{"title":"liteyuki.bot.lifespan","description":"","frontmatter":{"title":"liteyuki.bot.lifespan"},"headers":[],"relativePath":"dev/api/bot/lifespan.md","filePath":"zh/dev/api/bot/lifespan.md","lastUpdated":null}'),e={name:"dev/api/bot/lifespan.md"};function l(h,s,p,r,k,o){return t(),a("div",null,s[0]||(s[0]=[n('
liteyuki.bot.lifespan
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@Time : 2024/7/23 下午8:24 @Author : snowykami @Email : snowykami@outlook.com @File : lifespan.py @Software: PyCharm
SYNC_LIFESPAN_FUNC
说明: 同步生命周期函数
类型: TypeAlias
默认值: Callable[[], Any]
ASYNC_LIFESPAN_FUNC
说明: 异步生命周期函数
类型: TypeAlias
默认值: Callable[[], Awaitable[Any]]
LIFESPAN_FUNC
说明: 生命周期函数
类型: TypeAlias
默认值: SYNC_LIFESPAN_FUNC | ASYNC_LIFESPAN_FUNC
SYNC_PROCESS_LIFESPAN_FUNC
说明: 同步进程生命周期函数
类型: TypeAlias
默认值: Callable[[str], Any]
ASYNC_PROCESS_LIFESPAN_FUNC
说明: 异步进程生命周期函数
类型: TypeAlias
默认值: Callable[[str], Awaitable[Any]]
PROCESS_LIFESPAN_FUNC
说明: 进程函数
类型: TypeAlias
默认值: SYNC_PROCESS_LIFESPAN_FUNC | ASYNC_PROCESS_LIFESPAN_FUNC
Lifespan
__init__(self) -> None
说明: 轻雪生命周期管理,启动、停止、重启
def __init__(self) -> None:\n self.life_flag: int = 0\n self._before_start_funcs: list[LIFESPAN_FUNC] = []\n self._after_start_funcs: list[LIFESPAN_FUNC] = []\n self._before_process_shutdown_funcs: list[PROCESS_LIFESPAN_FUNC] = []\n self._after_shutdown_funcs: list[LIFESPAN_FUNC] = []\n self._before_process_restart_funcs: list[PROCESS_LIFESPAN_FUNC] = []\n self._after_restart_funcs: list[LIFESPAN_FUNC] = []
run_funcs(funcs: Sequence[LIFESPAN_FUNC | PROCESS_LIFESPAN_FUNC], *args, **kwargs) -> None
说明: 并发运行异步函数
参数:
- funcs (
Sequence
[ASYNC_LIFESPAN_FUNC
|PROCESS_LIFESPAN_FUNC
]): 函数列表
@staticmethod\nasync def run_funcs(funcs: Sequence[LIFESPAN_FUNC | PROCESS_LIFESPAN_FUNC], *args, **kwargs) -> None:\n tasks = [func(*args, **kwargs) if is_coroutine_callable(func) else async_wrapper(func)(*args, **kwargs) for func in funcs]\n await asyncio.gather(*tasks)
on_before_start(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC
说明: 注册启动时的函数
参数:
- func (
LIFESPAN_FUNC
): 生命周期函数
返回: LIFESPAN_FUNC
: 生命周期函数
def on_before_start(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:\n self._before_start_funcs.append(func)\n return func
on_after_start(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC
说明: 注册启动时的函数
参数:
- func (
LIFESPAN_FUNC
): 生命周期函数
返回: LIFESPAN_FUNC
: 生命周期函数
def on_after_start(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:\n self._after_start_funcs.append(func)\n return func
on_before_process_shutdown(self, func: PROCESS_LIFESPAN_FUNC) -> PROCESS_LIFESPAN_FUNC
说明: 注册进程停止前的函数
参数:
- func (
PROCESS_LIFESPAN_FUNC
): 进程生命周期函数
返回: PROCESS_LIFESPAN_FUNC
: 进程生命周期函数
def on_before_process_shutdown(self, func: PROCESS_LIFESPAN_FUNC) -> PROCESS_LIFESPAN_FUNC:\n self._before_process_shutdown_funcs.append(func)\n return func
on_after_shutdown(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC
说明: 注册停止后的函数
参数:
- func (
LIFESPAN_FUNC
): 生命周期函数
返回: LIFESPAN_FUNC
: 生命周期函数
def on_after_shutdown(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:\n self._after_shutdown_funcs.append(func)\n return func
on_before_process_restart(self, func: PROCESS_LIFESPAN_FUNC) -> PROCESS_LIFESPAN_FUNC
说明: 注册进程重启前的函数
参数:
- func (
PROCESS_LIFESPAN_FUNC
): 进程生命周期函数
返回: PROCESS_LIFESPAN_FUNC
: 进程生命周期函数
def on_before_process_restart(self, func: PROCESS_LIFESPAN_FUNC) -> PROCESS_LIFESPAN_FUNC:\n self._before_process_restart_funcs.append(func)\n return func
on_after_restart(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC
说明: 注册重启后的函数
参数:
- func (
LIFESPAN_FUNC
): 生命周期函数
返回: LIFESPAN_FUNC
: 生命周期函数
def on_after_restart(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:\n self._after_restart_funcs.append(func)\n return func
before_start(self) -> None
说明: 启动前钩子
async def before_start(self) -> None:\n logger.debug('Running before_start functions')\n await self.run_funcs(self._before_start_funcs)
after_start(self) -> None
说明: 启动后钩子
async def after_start(self) -> None:\n logger.debug('Running after_start functions')\n await self.run_funcs(self._after_start_funcs)
before_process_shutdown(self, *args, **kwargs) -> None
说明: 停止前钩子
async def before_process_shutdown(self, *args, **kwargs) -> None:\n logger.debug('Running before_shutdown functions')\n await self.run_funcs(self._before_process_shutdown_funcs, *args, **kwargs)
after_shutdown(self) -> None
说明: 停止后钩子 未实现
async def after_shutdown(self) -> None:\n logger.debug('Running after_shutdown functions')\n await self.run_funcs(self._after_shutdown_funcs)
before_process_restart(self, *args, **kwargs) -> None
说明: 重启前钩子
async def before_process_restart(self, *args, **kwargs) -> None:\n logger.debug('Running before_restart functions')\n await self.run_funcs(self._before_process_restart_funcs, *args, **kwargs)
after_restart(self) -> None
说明: 重启后钩子 未实现
async def after_restart(self) -> None:\n logger.debug('Running after_restart functions')\n await self.run_funcs(self._after_restart_funcs)