import{_ as s,c as i,o as a,a6 as n}from"./chunks/framework.BcfwU5BI.js";const y=JSON.parse('{"title":"liteyuki.core.manager","description":"","frontmatter":{"title":"liteyuki.core.manager"},"headers":[],"relativePath":"dev/api/core/manager.md","filePath":"zh/dev/api/core/manager.md","lastUpdated":null}'),e={name:"dev/api/core/manager.md"},t=n(`
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@Time : 2024/7/27 上午11:12 @Author : snowykami @Email : snowykami@outlook.com @File : manager.py @Software: PyCharm
ChannelDeliver
__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
ProcessManager
__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] = {}
start_all(self)
说明: 对外启动方法,启动所有进程,创建asyncio task
def start_all(self):
"""
对外启动方法,启动所有进程,创建asyncio task
"""
for name in self.targets:
logger.debug(f'Starting process {name}')
threading.Thread(target=self._run_process, args=(name,), daemon=True).start()
add_target(self, name: str, target: TARGET_FUNC, args: tuple = (), kwargs = None)
说明: 添加进程
参数:
- 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(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)
join_all(self)
def join_all(self):
for name, process in self.targets:
process.join()
terminate(self, name: str)
说明: 终止进程并从进程字典中删除
参数:
- name:
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.')
terminate_all(self)
def terminate_all(self):
for name in self.targets:
self.terminate(name)
is_process_alive(self, name: str) -> bool
说明: 检查进程是否存活
参数:
- name:
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()