How to run QTcpServer in separate thread from GUI in PySide6?

368 Views Asked by At

I'm making a PySide6 program with a GUI, backend, and a QTcpServer (which only handles a single connection), and I want all three in separate threads as to never freeze up the GUI.

I read about implementing the run() function in QThread, but the backend and the server are entirely event-driven, so this seems like a bad solution. Is there a way to just place the whole QTcpServer and backend instances in separate threads?

1

There are 1 best solutions below

0
On

Solution

Note: My TCP_Server is a singleton, not sure if you would need a reference to it otherwise, so it doesn't go out of scope.

class ServerThread(QRunnable):
    @Slot()
    def run(self):
        TCP_Server()


threadpool = QThreadPool()
server = ServerThread()
threadpool.start(server)