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:
@ -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
|
||||
|
@ -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):
|
||||
|
Reference in New Issue
Block a user