0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 03:31:50 +02:00

fix(rpc_register): thread lock for listign all connections

This commit is contained in:
2024-04-25 17:39:01 +02:00
parent 381d713837
commit 2ca32675ec
2 changed files with 11 additions and 6 deletions

View File

@ -1,8 +1,9 @@
# This file was automatically generated by generate_cli.py
from bec_widgets.cli.client_utils import rpc_call, RPCBase, BECFigureClientMixin
from typing import Literal, Optional, overload
from bec_widgets.cli.client_utils import BECFigureClientMixin, RPCBase, rpc_call
class BECPlotBase(RPCBase):
@property

View File

@ -1,9 +1,11 @@
from threading import Lock
from weakref import WeakValueDictionary
class RPCRegister:
_instance = None
_initialized = False
_lock = Lock()
def __new__(cls, *args, **kwargs):
if cls._instance is None:
@ -14,26 +16,28 @@ class RPCRegister:
def __init__(self):
if self._initialized:
return
self.rpc_register = WeakValueDictionary()
self._rpc_register = WeakValueDictionary()
self._initialized = True
def add_rpc(self, rpc):
if not hasattr(rpc, "gui_id"):
raise ValueError("RPC object must have a 'gui_id' attribute.")
self.rpc_register[rpc.gui_id] = rpc
self._rpc_register[rpc.gui_id] = rpc
def remove_rpc(self, rpc):
if not hasattr(rpc, "gui_id"):
raise ValueError(f"RPC object {rpc} must have a 'gui_id' attribute.")
self.rpc_register.pop(rpc.gui_id, None)
self._rpc_register.pop(rpc.gui_id, None)
def get_rpc_by_id(self, gui_id):
rpc_object = self.rpc_register.get(gui_id, None)
rpc_object = self._rpc_register.get(gui_id, None)
print(f"get rpc by id: {rpc_object}")
return rpc_object
def list_all_connections(self):
return self.rpc_register
with self._lock:
connections = dict(self._rpc_register)
return connections
@classmethod
def reset_singleton(cls):