对通道类添加类型检查和泛型

This commit is contained in:
远野千束 2024-08-17 19:12:11 +08:00
parent 8e27f6b9b0
commit 48085a946d

View File

@ -155,26 +155,33 @@ class GlobalKeyValueStore:
if IS_MAIN_PROCESS:
shared_memory: KeyValueStore = GlobalKeyValueStore.get_instance()
@shared_memory.passive_chan.on_receive(lambda d: d[0] == "get")
def on_get():
# TODO
pass
def on_get(data: tuple[str, dict[str, Any]]):
key = data[1]["key"]
default = data[1]["default"]
recv_chan = data[1]["recv_chan"]
recv_chan.send(shared_memory.get(key, default))
@shared_memory.passive_chan.on_receive(lambda d: d[0] == "set")
def on_set(data: tuple[str, str, Any]):
shared_memory.set(data[1], data[2])
def on_set(data: tuple[str, dict[str, Any]]):
key = data[1]["key"]
value = data[1]["value"]
shared_memory.set(key, value)
@shared_memory.passive_chan.on_receive(lambda d: d[0] == "delete")
def on_delete(data: tuple[str, str]):
shared_memory.delete(data[1])
def on_delete(data: tuple[str, dict[str, Any]]):
key = data[1]["key"]
shared_memory.delete(key)
@shared_memory.passive_chan.on_receive(lambda d: d[0] == "get_all")
def on_get_all(data: tuple[str, Channel]):
if data[0] == "get_all":
data[1].send(shared_memory.get_all())
def on_get_all(data: tuple[str, dict[str, Any]]):
recv_chan = data[1]["recv_chan"]
recv_chan.send(shared_memory.get_all())
else:
# 子进程在入口函数中对shared_memory进行初始化
shared_memory: Optional[KeyValueStore] = None # type: ignore