Skip to content

Commit 3d45997

Browse files
committed
add close() method for sync client
Signed-off-by: 0oshowero0 <o0shower0o@outlook.com>
1 parent f92e9df commit 3d45997

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

transfer_queue/client.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ def __init__(
809809
controller_info,
810810
)
811811

812-
# create new event loop in a separated thread
812+
# create new event loop in a separate thread
813813
self._loop = asyncio.new_event_loop()
814814
self._thread = threading.Thread(target=self._start_loop, daemon=True)
815815
self._thread.start()
@@ -1006,6 +1006,24 @@ def get_partition_list(
10061006
"""
10071007
return self._get_partition_list()
10081008

1009+
def close(self) -> None:
1010+
"""Close the client and cleanup resources including event loop and thread."""
1011+
1012+
if hasattr(self, "_loop") and self._loop is not None:
1013+
self._loop.call_soon_threadsafe(self._loop.stop)
1014+
1015+
if hasattr(self, "_thread") and self._thread is not None:
1016+
self._thread.join(timeout=5.0)
1017+
if self._thread.is_alive():
1018+
logger.warning(f"[{self.client_id}]: Background thread did not stop within timeout")
1019+
1020+
try:
1021+
self._loop.close()
1022+
except Exception as e:
1023+
logger.warning(f"[{self.client_id}]: Error closing event loop: {e}")
1024+
1025+
super().close()
1026+
10091027

10101028
def process_zmq_server_info(
10111029
handlers: dict[Any, Union["TransferQueueController", "TransferQueueStorageManager", "SimpleStorageUnit"]]

0 commit comments

Comments
 (0)