mirror of
https://github.com/LiteyukiStudio/LiteyukiBot.git
synced 2024-11-22 16:47:41 +08:00
🐛 fix: Channel的接收者过滤器的问题,优化重启部分
This commit is contained in:
parent
ca34f9c2a1
commit
6ba983fae3
@ -3,7 +3,6 @@ import asyncio
|
||||
from typing import Any, Optional
|
||||
from multiprocessing import freeze_support
|
||||
|
||||
|
||||
from liteyuki.bot.lifespan import (LIFESPAN_FUNC, Lifespan)
|
||||
from liteyuki.comm.channel import Channel
|
||||
from liteyuki.core import IS_MAIN_PROCESS
|
||||
@ -32,7 +31,6 @@ class LiteyukiBot:
|
||||
self.chan = Channel() # 进程通信通道
|
||||
self.pm: Optional[ProcessManager] = None # 启动时实例化
|
||||
|
||||
|
||||
print("\033[34m" + r"""
|
||||
__ ______ ________ ________ __ __ __ __ __ __ ______
|
||||
/ | / |/ |/ |/ \ / |/ | / |/ | / |/ |
|
||||
@ -49,12 +47,12 @@ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
|
||||
# load_plugins("liteyuki/plugins") # 加载轻雪插件
|
||||
self.pm = ProcessManager(bot=self, chan=self.chan)
|
||||
|
||||
self.pm.add_target("melobot", mb_run, **self.config)
|
||||
self.pm.start("melobot")
|
||||
|
||||
self.pm.add_target("nonebot", nb_run, **self.config)
|
||||
self.pm.start("nonebot")
|
||||
|
||||
self.pm.add_target("melobot", mb_run, **self.config)
|
||||
self.pm.start("melobot")
|
||||
|
||||
run_coroutine(self.lifespan.after_start()) # 启动前
|
||||
|
||||
def restart(self, name: Optional[str] = None):
|
||||
|
@ -31,7 +31,7 @@ class Channel:
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.parent_conn, self.child_conn = Pipe()
|
||||
self.receive_conn, self.send_conn = Pipe()
|
||||
self._closed = False
|
||||
self._on_receive_funcs: List[ON_RECEIVE_FUNC] = []
|
||||
self._on_receive_funcs_with_receiver: dict[str, List[ON_RECEIVE_FUNC]] = {}
|
||||
@ -45,7 +45,7 @@ class Channel:
|
||||
"""
|
||||
if self._closed:
|
||||
raise RuntimeError("Cannot send to a closed channel")
|
||||
self.child_conn.send((data, receiver))
|
||||
self.send_conn.send((data, receiver))
|
||||
|
||||
def receive(self, receiver: str = None) -> Any:
|
||||
"""
|
||||
@ -57,11 +57,11 @@ class Channel:
|
||||
raise RuntimeError("Cannot receive from a closed channel")
|
||||
while True:
|
||||
# 判断receiver是否为None或者receiver是否等于接收者,是则接收数据,否则不动数据
|
||||
if self.parent_conn.poll():
|
||||
data, receiver = self.parent_conn.recv()
|
||||
self.parent_conn.send((data, receiver))
|
||||
self._run_on_receive_funcs(data, receiver)
|
||||
data, receiver_ = self.receive_conn.recv()
|
||||
if receiver is None or receiver == receiver_:
|
||||
self._run_on_receive_funcs(data, receiver_)
|
||||
return data
|
||||
self.send_conn.send((data, receiver_))
|
||||
|
||||
def peek(self) -> Optional[Any]:
|
||||
"""
|
||||
@ -70,9 +70,9 @@ class Channel:
|
||||
"""
|
||||
if self._closed:
|
||||
raise RuntimeError("Cannot peek from a closed channel")
|
||||
if self.parent_conn.poll():
|
||||
data, receiver = self.parent_conn.recv()
|
||||
self.parent_conn.send((data, receiver))
|
||||
if self.receive_conn.poll():
|
||||
data, receiver = self.receive_conn.recv()
|
||||
self.receive_conn.send((data, receiver))
|
||||
return data
|
||||
return None
|
||||
|
||||
@ -81,8 +81,8 @@ class Channel:
|
||||
关闭通道
|
||||
"""
|
||||
self._closed = True
|
||||
self.parent_conn.close()
|
||||
self.child_conn.close()
|
||||
self.receive_conn.close()
|
||||
self.send_conn.close()
|
||||
|
||||
def on_receive(self, filter_func: Optional[FILTER_FUNC] = None, receiver: Optional[str] = None) -> Callable[[ON_RECEIVE_FUNC], ON_RECEIVE_FUNC]:
|
||||
"""
|
||||
|
@ -53,7 +53,7 @@ class ProcessManager:
|
||||
while not should_exit:
|
||||
# 0退出 1重启
|
||||
data = self.chan.receive(name)
|
||||
print("Received data: ", data)
|
||||
print("Received data: ", data, name)
|
||||
if data == 1:
|
||||
logger.info("Restarting LiteyukiBot...")
|
||||
process.terminate()
|
||||
|
Loading…
Reference in New Issue
Block a user