import{_ as n,o as s,c as a,e}from"./app-BrSLFMno.js";const t={},p=e(`

class ChannelDeliver

method __init__(self, active: Channel[Any], passive: Channel[Any], channel_deliver_active: Channel[Channel[Any]], channel_deliver_passive: Channel[tuple[str, dict]], publish: Channel[tuple[str, Any]])

源代码
def __init__(self, active: Channel[Any], passive: Channel[Any], channel_deliver_active: Channel[Channel[Any]], channel_deliver_passive: Channel[tuple[str, dict]], publish: Channel[tuple[str, Any]]):
    self.active = active
    self.passive = passive
    self.channel_deliver_active = channel_deliver_active
    self.channel_deliver_passive = channel_deliver_passive
    self.publish = publish

class ProcessManager

method __init__(self, lifespan: Lifespan)

源代码
def __init__(self, lifespan: 'Lifespan'):
    self.lifespan = lifespan
    self.targets: dict[str, tuple[Callable, tuple, dict]] = {}
    self.processes: dict[str, Process] = {}

async method _run_process(self, name: str)

说明: 开启后自动监控进程,并添加到进程字典中,会阻塞,请创建task

参数:

源代码
async def _run_process(self, name: str):
    """
        开启后自动监控进程,并添加到进程字典中,会阻塞,请创建task
        Args:
            name:
        Returns:
        """
    if name not in self.targets:
        raise KeyError(f'Process {name} not found.')
    chan_active = get_channel(f'{name}-active')

    def _start_process():
        process = Process(target=self.targets[name][0], args=self.targets[name][1], kwargs=self.targets[name][2], daemon=True)
        self.processes[name] = process
        process.start()
    _start_process()
    while True:
        data = await chan_active.async_receive()
        if data == 0:
            logger.info(f'Stopping process {name}')
            await self.lifespan.before_process_shutdown()
            self.terminate(name)
            break
        elif data == 1:
            logger.info(f'Restarting process {name}')
            await self.lifespan.before_process_shutdown()
            await self.lifespan.before_process_restart()
            self.terminate(name)
            _start_process()
            continue
        else:
            logger.warning('Unknown data received, ignored.')

async method start_all(self)

说明: 对外启动方法,启动所有进程,创建asyncio task

源代码
async def start_all(self):
    """
        对外启动方法,启动所有进程,创建asyncio task
        """
    [asyncio.create_task(chan.start_receive_loop()) for chan in get_channels().values()]
    [asyncio.create_task(sm.start_receive_loop()) for sm in [shared_memory]]
    [asyncio.create_task(self._run_process(name)) for name in self.targets]

method add_target(self, name: str, target: TARGET_FUNC, args: tuple = (), kwargs = None)

说明: 添加进程

参数:

源代码
def add_target(self, name: str, target: TARGET_FUNC, args: tuple=(), kwargs=None):
    """
        添加进程
        Args:
            name: 进程名,用于获取和唯一标识
            target: 进程函数
            args: 进程函数参数
            kwargs: 进程函数关键字参数,通常会默认传入chan_active和chan_passive
        """
    if kwargs is None:
        kwargs = {}
    chan_active: Channel = Channel(name=f'{name}-active')
    chan_passive: Channel = Channel(name=f'{name}-passive')
    channel_deliver = ChannelDeliver(active=chan_active, passive=chan_passive, channel_deliver_active=channel_deliver_active_channel, channel_deliver_passive=channel_deliver_passive_channel, publish=publish_channel)
    self.targets[name] = (_delivery_channel_wrapper, (target, channel_deliver, shared_memory, *args), kwargs)

method join_all(self)

源代码
def join_all(self):
    for (name, process) in self.targets:
        process.join()

method terminate(self, name: str)

说明: 终止进程并从进程字典中删除

参数:

源代码
def terminate(self, name: str):
    """
        终止进程并从进程字典中删除
        Args:
            name:

        Returns:

        """
    if name not in self.processes:
        logger.warning(f'Process {name} not found.')
        return
    process = self.processes[name]
    process.terminate()
    process.join(TIMEOUT)
    if process.is_alive():
        process.kill()
    logger.success(f'Process {name} terminated.')

method terminate_all(self)

源代码
def terminate_all(self):
    for name in self.targets:
        self.terminate(name)

method is_process_alive(self, name: str) -> bool

说明: 检查进程是否存活

参数:

源代码
def is_process_alive(self, name: str) -> bool:
    """
        检查进程是否存活
        Args:
            name:

        Returns:

        """
    if name not in self.targets:
        logger.warning(f'Process {name} not found.')
    return self.processes[name].is_alive()

var TARGET_FUNC = Callable[..., Any]

`,35),o=[p];function l(c,i){return s(),a("div",null,o)}const r=n(t,[["render",l],["__file","manager.html.vue"]]),d=JSON.parse('{"path":"/dev/api/core/manager.html","title":"liteyuki.core.manager","lang":"zh-CN","frontmatter":{"title":"liteyuki.core.manager","description":"class ChannelDeliver method __init__(self, active: Channel[Any], passive: Channel[Any], channel_deliver_active: Channel[Channel[Any]], channel_deliver_passive: Channel[tuple[str...","head":[["link",{"rel":"alternate","hreflang":"en-us","href":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/api/core/manager.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/api/core/manager.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot 轻雪机器人"}],["meta",{"property":"og:title","content":"liteyuki.core.manager"}],["meta",{"property":"og:description","content":"class ChannelDeliver method __init__(self, active: Channel[Any], passive: Channel[Any], channel_deliver_active: Channel[Channel[Any]], channel_deliver_passive: Channel[tuple[str..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:locale:alternate","content":"en-US"}],["meta",{"property":"og:updated_time","content":"2024-08-29T06:19:39.000Z"}],["meta",{"property":"article:modified_time","content":"2024-08-29T06:19:39.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"liteyuki.core.manager\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-29T06:19:39.000Z\\",\\"author\\":[]}"]]},"headers":[{"level":3,"title":"class ChannelDeliver","slug":"class-channeldeliver","link":"#class-channeldeliver","children":[]},{"level":3,"title":"method __init__(self, active: Channel[Any], passive: Channel[Any], channel_deliver_active: Channel[Channel[Any]], channel_deliver_passive: Channel[tuple[str, dict]], publish: Channel[tuple[str, Any]])","slug":"method-init-self-active-channel-any-passive-channel-any-channel-deliver-active-channel-channel-any-channel-deliver-passive-channel-tuple-str-dict-publish-channel-tuple-str-any","link":"#method-init-self-active-channel-any-passive-channel-any-channel-deliver-active-channel-channel-any-channel-deliver-passive-channel-tuple-str-dict-publish-channel-tuple-str-any","children":[]},{"level":3,"title":"class ProcessManager","slug":"class-processmanager","link":"#class-processmanager","children":[]},{"level":3,"title":"method __init__(self, lifespan: Lifespan)","slug":"method-init-self-lifespan-lifespan","link":"#method-init-self-lifespan-lifespan","children":[]},{"level":3,"title":"async method _run_process(self, name: str)","slug":"async-method-run-process-self-name-str","link":"#async-method-run-process-self-name-str","children":[]},{"level":3,"title":"async method start_all(self)","slug":"async-method-start-all-self","link":"#async-method-start-all-self","children":[]},{"level":3,"title":"method add_target(self, name: str, target: TARGET_FUNC, args: tuple = (), kwargs = None)","slug":"method-add-target-self-name-str-target-target-func-args-tuple-kwargs-none","link":"#method-add-target-self-name-str-target-target-func-args-tuple-kwargs-none","children":[]},{"level":3,"title":"method join_all(self)","slug":"method-join-all-self","link":"#method-join-all-self","children":[]},{"level":3,"title":"method terminate(self, name: str)","slug":"method-terminate-self-name-str","link":"#method-terminate-self-name-str","children":[]},{"level":3,"title":"method terminate_all(self)","slug":"method-terminate-all-self","link":"#method-terminate-all-self","children":[]},{"level":3,"title":"method is_process_alive(self, name: str) -> bool","slug":"method-is-process-alive-self-name-str-bool","link":"#method-is-process-alive-self-name-str-bool","children":[]},{"level":3,"title":"var TARGET_FUNC = Callable[..., Any]","slug":"var-target-func-callable-any","link":"#var-target-func-callable-any","children":[]}],"git":{"createdTime":1724031826000,"updatedTime":1724912379000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":6}]},"readingTime":{"minutes":2.35,"words":704},"filePathRelative":"dev/api/core/manager.md","localizedDate":"2024年8月19日","autoDesc":true}');export{r as comp,d as data};