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

class ChannelDeliver

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]]) -> None

源代码
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

进程管理器

def __init__(self, lifespan: 'Lifespan') -> None

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

def start(self, name: str) -> None

 开启后自动监控进程,并添加到进程字典中

Args:

name:

Returns:

源代码
def start(self, name: str):
    """
        开启后自动监控进程,并添加到进程字典中
        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 = chan_active.receive()
        if data == 0:
            logger.info(f'Stopping process {name}')
            self.lifespan.before_process_shutdown()
            self.terminate(name)
            break
        elif data == 1:
            logger.info(f'Restarting process {name}')
            self.lifespan.before_process_shutdown()
            self.lifespan.before_process_restart()
            self.terminate(name)
            _start_process()
            continue
        else:
            logger.warning('Unknown data received, ignored.')

def start_all(self) -> None

 启动所有进程

源代码
def start_all(self):
    """
        启动所有进程
        """
    for name in self.targets:
        threading.Thread(target=self.start, args=(name,), daemon=True).start()

def add_target(self, name: str, target: TARGET_FUNC, args: tuple, kwargs: Any) -> None

 添加进程

Args:

name: 进程名,用于获取和唯一标识

target: 进程函数

args: 进程函数参数

kwargs: 进程函数关键字参数,通常会默认传入chan_active和chan_passive
源代码
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(_id=f'{name}-active')
    chan_passive: Channel = Channel(_id=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)
    set_channels({f'{name}-active': chan_active, f'{name}-passive': chan_passive})

def join_all(self) -> None

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

def terminate(self, name: str) -> None

 终止进程并从进程字典中删除

Args:

name:

Returns:

源代码
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.')

def terminate_all(self) -> None

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

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

 检查进程是否存活

Args:

name:

Returns:

源代码
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 TIMEOUT = 10

var chan_active = get_channel(f'{name}-active')

var 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)

var process = self.processes[name]

var process = Process(target=self.targets[name][0], args=self.targets[name][1], kwargs=self.targets[name][2], daemon=True)

var data = chan_active.receive()

var kwargs = {}

`,48),l=[p];function c(o,i){return s(),a("div",null,l)}const u=n(t,[["render",c],["__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","order":1,"icon":"laptop-code","category":"API","description":"class ChannelDeliver def __init__(self, active: Channel[Any], passive: Channel[Any], channel_deliver_active: Channel[Channel[Any]], channel_deliver_passive: Channel[tuple[str, d...","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 def __init__(self, active: Channel[Any], passive: Channel[Any], channel_deliver_active: Channel[Channel[Any]], channel_deliver_passive: Channel[tuple[str, d..."}],["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-21T09:59:21.000Z"}],["meta",{"property":"article:modified_time","content":"2024-08-21T09:59:21.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"liteyuki.core.manager\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-21T09:59:21.000Z\\",\\"author\\":[]}"]]},"headers":[{"level":3,"title":"class ChannelDeliver","slug":"class-channeldeliver","link":"#class-channeldeliver","children":[]},{"level":3,"title":"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]]) -> None","slug":"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-none","link":"#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-none","children":[]},{"level":3,"title":"class ProcessManager","slug":"class-processmanager","link":"#class-processmanager","children":[]},{"level":3,"title":"def __init__(self, lifespan: 'Lifespan') -> None","slug":"def-init-self-lifespan-lifespan-none","link":"#def-init-self-lifespan-lifespan-none","children":[]},{"level":3,"title":"def start(self, name: str) -> None","slug":"def-start-self-name-str-none","link":"#def-start-self-name-str-none","children":[]},{"level":3,"title":"def start_all(self) -> None","slug":"def-start-all-self-none","link":"#def-start-all-self-none","children":[]},{"level":3,"title":"def add_target(self, name: str, target: TARGET_FUNC, args: tuple, kwargs: Any) -> None","slug":"def-add-target-self-name-str-target-target-func-args-tuple-kwargs-any-none","link":"#def-add-target-self-name-str-target-target-func-args-tuple-kwargs-any-none","children":[]},{"level":3,"title":"def join_all(self) -> None","slug":"def-join-all-self-none","link":"#def-join-all-self-none","children":[]},{"level":3,"title":"def terminate(self, name: str) -> None","slug":"def-terminate-self-name-str-none","link":"#def-terminate-self-name-str-none","children":[]},{"level":3,"title":"def terminate_all(self) -> None","slug":"def-terminate-all-self-none","link":"#def-terminate-all-self-none","children":[]},{"level":3,"title":"def is_process_alive(self, name: str) -> bool","slug":"def-is-process-alive-self-name-str-bool","link":"#def-is-process-alive-self-name-str-bool","children":[]},{"level":3,"title":"var TIMEOUT = 10","slug":"var-timeout-10","link":"#var-timeout-10","children":[]},{"level":3,"title":"var chan_active = get_channel(f'{name}-active')","slug":"var-chan-active-get-channel-f-name-active","link":"#var-chan-active-get-channel-f-name-active","children":[]},{"level":3,"title":"var 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)","slug":"var-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","link":"#var-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","children":[]},{"level":3,"title":"var process = self.processes[name]","slug":"var-process-self-processes-name","link":"#var-process-self-processes-name","children":[]},{"level":3,"title":"var process = Process(target=self.targets[name][0], args=self.targets[name][1], kwargs=self.targets[name][2], daemon=True)","slug":"var-process-process-target-self-targets-name-0-args-self-targets-name-1-kwargs-self-targets-name-2-daemon-true","link":"#var-process-process-target-self-targets-name-0-args-self-targets-name-1-kwargs-self-targets-name-2-daemon-true","children":[]},{"level":3,"title":"var data = chan_active.receive()","slug":"var-data-chan-active-receive","link":"#var-data-chan-active-receive","children":[]},{"level":3,"title":"var kwargs = {}","slug":"var-kwargs","link":"#var-kwargs","children":[]}],"git":{"createdTime":1724031826000,"updatedTime":1724234361000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":5}]},"readingTime":{"minutes":2.37,"words":712},"filePathRelative":"dev/api/core/manager.md","localizedDate":"2024年8月19日","autoDesc":true}`);export{u as comp,d as data};