From d0d33f42da232b126a3d9350a464e29ac9d85d73 Mon Sep 17 00:00:00 2001 From: atse Date: Thu, 6 Jul 2023 11:21:56 +0800 Subject: [PATCH] Rearrange client_watcher to hold its own task --- pytec/tec_qt.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pytec/tec_qt.py b/pytec/tec_qt.py index 1d87637..23b81a9 100644 --- a/pytec/tec_qt.py +++ b/pytec/tec_qt.py @@ -38,6 +38,7 @@ class ClientWatcher(QObject): self.update_s = update_s self.running = True self.client = client + self.watch_task = None super().__init__(parent) async def run(self): @@ -50,6 +51,9 @@ class ClientWatcher(QObject): async def update_params(self): self.fan_update.emit(await self.client.fan()) + def start_watching(self): + self.watch_task = asyncio.create_task(self.run()) + @pyqtSlot() def stop_watching(self): self.running = False @@ -71,7 +75,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): self.tec_client: Client = None self.client_watcher: ClientWatcher = None - self.client_watcher_task = None if args.connect: if args.IP: @@ -149,16 +152,15 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): lambda: self.client_watcher.set_update_s(self.report_refresh_spin.value()) ) QtWidgets.QApplication.instance().aboutToQuit.connect(self.client_watcher.stop_watching) - self.client_watcher_task = asyncio.create_task(self.client_watcher.run()) + self.client_watcher.start_watching() else: + self.client_watcher.stop_watching() + self.client_watcher = None + await self.tec_client.disconnect() self.tec_client = None self._on_connection_changed(False) - self.client_watcher.stop_watching() - self.client_watcher = None - await self.client_watcher_task - self.client_watcher_task = None except Exception as e: logging.error(f"Failed communicating to the {ip}:{port}: {e}") self._on_connection_changed(False)