diff --git a/docs/user-guide/interaction/Python-Client.md b/docs/user-guide/interaction/Python-Client.md index 5881e66..e180ce1 100644 --- a/docs/user-guide/interaction/Python-Client.md +++ b/docs/user-guide/interaction/Python-Client.md @@ -23,6 +23,19 @@ The proxy acts as a local representation of the remote service, enabling intuiti The proxy class automatically synchronizes with the server's attributes and methods, keeping itself up-to-date with any changes. This dynamic synchronization essentially mirrors the server's API, making it feel like you're working with a local object. +## Automatic Proxy Updates + +By default, the client listens for attribute and structure changes from the server and dynamically updates its internal proxy representation. This ensures that value changes or newly added attributes on the server appear in the client proxy without requiring reconnection or manual refresh. + +This is useful, for example, when [integrating the client into another service](#integrating-the-client-into-another-service). However, if you want to avoid this behavior (e.g., to reduce network traffic or avoid frequent re-syncing), you can disable it. When passing `auto_update_proxy=False` to the client, the proxy will not track changes after the initial connection: + +```python +client = pydase.Client( + url="ws://localhost:8001", + auto_update_proxy=False +) +``` + ## Direct API Access In addition to using the `proxy` object, users may access the server API directly via the following methods: @@ -94,6 +107,7 @@ if __name__ == "__main__": ``` In this example: + - The `MyService` class has a `proxy` attribute that connects to a `pydase` service at `:`. - By setting `block_until_connected=False`, the service can start without waiting for the connection to succeed. - The `client_id` is optional. If not specified, it defaults to the system hostname, which will be sent in the `X-Client-Id` HTTP header for logging or authentication on the server side. diff --git a/src/pydase/client/client.py b/src/pydase/client/client.py index 2deb715..b799c51 100644 --- a/src/pydase/client/client.py +++ b/src/pydase/client/client.py @@ -70,6 +70,8 @@ class Client: proxy_url: An optional proxy URL to route the connection through. This is useful if the service is only reachable via an SSH tunnel or behind a firewall (e.g., `socks5://localhost:2222`). + auto_update_proxy: If False, disables automatic updates from the server. Useful + for request-only clients where real-time synchronization is not needed. Example: Connect to a service directly: @@ -98,7 +100,7 @@ class Client: ``` """ - def __init__( + def __init__( # noqa: PLR0913 self, *, url: str, @@ -106,6 +108,7 @@ class Client: sio_client_kwargs: dict[str, Any] = {}, client_id: str | None = None, proxy_url: str | None = None, + auto_update_proxy: bool = True, # new argument ): # Parse the URL to separate base URL and path prefix parsed_url = urllib.parse.urlparse(url) @@ -123,6 +126,7 @@ class Client: self._sio_client_kwargs = sio_client_kwargs self._loop: asyncio.AbstractEventLoop | None = None self._thread: threading.Thread | None = None + self._auto_update_proxy = auto_update_proxy self.proxy: ProxyClass """A proxy object representing the remote service, facilitating interaction as if it were local.""" @@ -229,23 +233,25 @@ class Client: async def _setup_events(self) -> None: self._sio.on("connect", self._handle_connect) self._sio.on("disconnect", self._handle_disconnect) - self._sio.on("notify", self._handle_update) + if self._auto_update_proxy: + self._sio.on("notify", self._handle_update) async def _handle_connect(self) -> None: logger.debug("Connected to '%s' ...", self._url) - serialized_object = cast( - "SerializedDataService", await self._sio.call("service_serialization") - ) - ProxyLoader.update_data_service_proxy( - self.proxy, serialized_object=serialized_object - ) - serialized_object["type"] = "DeviceConnection" - # need to use object.__setattr__ to not trigger an observer notification - object.__setattr__(self.proxy, "_service_representation", serialized_object) + if self._auto_update_proxy: + serialized_object = cast( + "SerializedDataService", await self._sio.call("service_serialization") + ) + ProxyLoader.update_data_service_proxy( + self.proxy, serialized_object=serialized_object + ) + serialized_object["type"] = "DeviceConnection" + # need to use object.__setattr__ to not trigger an observer notification + object.__setattr__(self.proxy, "_service_representation", serialized_object) - if TYPE_CHECKING: - self.proxy._service_representation = serialized_object # type: ignore - self.proxy._notify_changed("", self.proxy) + if TYPE_CHECKING: + self.proxy._service_representation = serialized_object # type: ignore + self.proxy._notify_changed("", self.proxy) self.proxy._connected = True async def _handle_disconnect(self) -> None: