'Cannot stop twisted reactor when calling reactor.stop()

I'm using twisted, PySide2 and qt5reactor to make a simple program that sends data through network. I want the reactor to stop when I closed the PySide2 window:

class MyWindow(QWidget):
    --snip--
    def closeEvent(self, event):
        super().closeEvent(event)
        reactor.callFromThread(reactor.stop)

However, the code above don't work at all. It don't produce any errors, but after the window closes, the program don't stop. It just closes the connection to the server. That means the reactor haven't stopped.

I've tried to call sys.exit() after calling reactor.callFromThread(...), but it was no use either. This time, the window also refuses to shut down! The whole program turned unresponsive, but no errors.

So how do I shut down the reactor cleanly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source