I have a QMainWindow application launched from shell, when it is launched I want it to also run the tcpserver, so I used multiprocess. Next I wanted when I close the QMainWindow it should also close the tcp server process. but instead it stays open. I have tried to use close_event initialised at the module level. but that did not help.
import os
import multiprocessing
import sys
from remote_tcp import main as _run_server
exit_event = multiprocessing.Event()
class MyQMainWindow(QMainWindow):
def __init__(self):
super().__init__()
self._init_ui()
# self.triggers()
self.connections()
def _init_ui(self):
loader = QUiLoader()
app_path = os.path.dirname(__file__)
ui_file = QFile(os.path.join(app_path, "resources", "utex.ui"))
ui_file.open(QFile.ReadOnly)
self.ui = loader.load(ui_file)
ui_file.close()
self.setCentralWidget(self.ui)
def closeEvent(self, event):
exit_event.set()
super().closeEvent(event)
def app_launcher(event):
app = QApplication(sys.argv)
window = MyQMainWindow()
window.closeEvent = lambda event: exit_event.set()
window.show()
app.exec_()
def tcp_server_launcher(event):
os.environ["PROMETHEUS_MULTIPROC_DIR"] = "/tmp/"
event.set()
_run_tcp_server()
def main():
tcp_server_process = multiprocessing.Process(target=tcp_server_launcher, args=(exit_event,))
main_window_process = multiprocessing.Process(target=app_launcher, args=(exit_event,))
# Start the processes
tcp_server_process.start()
main_window_process.start()
# Wait for the QMainWindow to close or exit_event to be set
main_window_process.join()
# Signal the Obex server to exit
exit_event.set()
# Wait for the TCP server to finish
tcp_server_process.join()
print("You are offline.")
if __name__ == "__main__":
sys.exit(main())