Search code examples
qtuser-interfacewebsocketpyqtqthread

QWebSocketServer to different thread


I have an application where i should use QWebSocketServer to accept and parse some socket messages coming from a Bokeh Application. I was able to do that up to now, but now i want to move this whole QWebSocketServer into another thread, so that it wont bother the main GUI. The main Question being does the QWebSocketServer in anyway affect GUI?

The GUI being the parent for QWebSocketServer, i was not able to move it to another thread. Will this work if i inherit QThread class?

I know i should not inherit QThread class and overload run method. I am just wondering if this is possible.

FYI : I am doing all of this in Raspberry pi, and raspberry pi has to do lot of data collection.


Solution

  • I initialized the QWebSocketServer without parent and moved it to another thread and it worked. I don't know if im doing right.

    Im doing like this :-

        self.server = SocketServer()
        self.serverThread = QThread()
        self.server.moveToThread(self.serverThread)
    

    Here SocketServer is the server class that inherits QObject(), and QWebSocketServer gets defined in it, with out any parent.

    Here is a short snippet of SocketServer class:-

        onMessageRecieved = pyqtSignal(str)
        serverObject = QWebSocketServer("My Server",QWebSocketServer.NonSecureMode)
        def __init__(self,parent=None):
            super().__init__(parent)
            self.server = QWebSocketServer(self.serverObject.serverName(),self.serverObject.secureMode(),parent)
    

    and dont forget to start the thread.