From 43f83d0cadd43719f265b399ba78d4d00719769e Mon Sep 17 00:00:00 2001 From: "Hailu, Mikael" Date: Mon, 11 Dec 2023 20:02:58 +0100 Subject: [PATCH] Refactor ThreadLoop reinitializing with decorators --- asyncua/sync.py | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/asyncua/sync.py b/asyncua/sync.py index b8cde51f5..b7c188cf7 100644 --- a/asyncua/sync.py +++ b/asyncua/sync.py @@ -113,6 +113,19 @@ def wrapper(self, *args, **kwargs): return wrapper +def sync_connector(func): + """ + decorator for sync methods that may need to reinitialize a ThreadLoop after being disconnected + """ + def decorator(self, *args, **kwargs): + if not self.tloop.is_alive(): + self.tloop = ThreadLoop() + self.tloop.start() + self.close_tloop = True + return syncmethod(func) + + return decorator + def sync_wrapper(aio_func): def wrapper(*args, **kwargs): @@ -249,12 +262,9 @@ def application_uri(self): def application_uri(self, value): self.aio_obj.application_uri = value + @sync_connector def connect(self) -> None: - if not self.tloop.is_alive(): - self.tloop = ThreadLoop() - self.tloop.start() - self.close_tloop = True - self.tloop.post(self.aio_obj.connect()) + pass def disconnect(self) -> None: try: @@ -263,12 +273,9 @@ def disconnect(self) -> None: if self.close_tloop: self.tloop.stop() + @sync_connector def connect_sessionless(self) -> None: - if not self.tloop.is_alive(): - self.tloop = ThreadLoop() - self.tloop.start() - self.close_tloop = True - self.tloop.post(self.aio_obj.connect_sessionless()) + pass def disconnect_sessionless(self) -> None: try: @@ -277,12 +284,9 @@ def disconnect_sessionless(self) -> None: if self.close_tloop: self.tloop.stop() + @sync_connector def connect_socket(self) -> None: - if not self.tloop.is_alive(): - self.tloop = ThreadLoop() - self.tloop.start() - self.close_tloop = True - self.tloop.post(self.aio_obj.connect_sessionless()) + pass def disconnect_socket(self) -> None: try: @@ -601,12 +605,9 @@ def register_namespace(self, url): def get_namespace_array(self): pass + @sync_connector def start(self): - if not self.tloop.is_alive(): - self.tloop = ThreadLoop() - self.tloop.start() - self.close_tloop = True - self.tloop.post(self.aio_obj.start()) + pass def stop(self): self.tloop.post(self.aio_obj.stop())