I'm running an application made with PyQt that basically reads information from an OPC server and updates a plot every time new data comes in.
I would like to be able to safely disconnect from the OPC server every time the application is closed. This includes the user manually closing the window and any runtime error that might occur. It would be something like this:
from opcua import Client
from matplotlib.backends.qt_compat import QtWidgets
class ApplicationWindow_Realtime(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(ApplicationWindow_Realtime, self).__init__(parent)
self.opc_url = 'opc.tcp://127.0.0.1:53530/UA/Sim'
self.opc_initialize()
## DO STUFF
## Connect to OPC
def opc_initialize(self):
self.client = Client(self.opc_url)
self.client.connect()
## OTHER METHODS
# Disconnect if window is closed
def closeEvent(self, event):
self.client.disconnect()
I want to know if there is some way to call self.client.disconnect()
if any error occurs in runtime when running the application. I found this question but the accepted answer starts with "Warning: if you want something like this, it's likely you don't... but if you really want to...", so I'm not sure if this is the right way to tackle the problem.