Skip to content

Commit

Permalink
Refactor ThreadLoop reinitializing with decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
miki5799 committed Dec 11, 2023
1 parent 94eaf78 commit 43f83d0
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions asyncua/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 43f83d0

Please sign in to comment.