Don't freeze the GUI thread on disconnect
ws_client.disconnect() blocks until the close handshake completes (observed ~10s when the server doesn't ack promptly), and was being called straight from the Connect/Disconnect button's click handler - joining that wait on the GUI thread, so the click appeared to do nothing until the timeout passed. Run it off a throwaway thread instead; on_ws_connect_changed() already flips the button once the disconnect actually finishes.
This commit is contained in:
@@ -3,6 +3,7 @@ from main_window import Ui_MainWindow
|
||||
from PyQt5 import QtWidgets, QtGui, QtCore
|
||||
import json
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import matplotlib
|
||||
matplotlib.use("Qt5Agg")
|
||||
@@ -246,7 +247,12 @@ class Window(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
self.plot_power_set, self.plot_power_eff)
|
||||
|
||||
def disconnect(self):
|
||||
self.ws_client.disconnect()
|
||||
# ws_client.disconnect() blocks until the websocket's close handshake
|
||||
# completes (or times out, ~10s if the server doesn't ack promptly),
|
||||
# joining the connection's background thread - run it off a throwaway
|
||||
# thread so that wait doesn't freeze the GUI. on_ws_connect_changed()
|
||||
# still flips the button once the disconnect actually finishes.
|
||||
threading.Thread(target=self.ws_client.disconnect, daemon=True).start()
|
||||
|
||||
def on_btn_connect_clicked(self):
|
||||
if self.connected:
|
||||
|
||||
Reference in New Issue
Block a user