添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am playing a bit with pyside2 and I wrote an application with a couple of threads that communicate between them using a queue. One thread reads from a serial port and passes the data to the other thread that parses them and does some calculations before plotting. From my app_main.py file:

import sys
# omissis various import statements
class MainWindow(QMainWindow, Ui_MainWindow):
    serialRxQu = Queue()  # serial FIFO RX Queue
    serialWo = ""
    def __init__(self, *args, **kwargs):
        Ui_MainWindow.__init__(self)
        QMainWindow.__init__(self)
        # omissis, various ui initializations
         # Visualization Worker Thread, started as soon as the thread pool is started.
        self.viewWo = ViewWorker(self.serialRxQu, self.canvas, self.textEdit)
        # serial Worker Thread started later
        self.serialWo = SerialWorker(self.serialRxQu, self.textEdit)
        self.threadpool = QThreadPool()
        self.threadpool.start(self.viewWo)
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

I omitted some parts that are not useful for the question and they would be difficult to explain without the inclusion of other files.

The application exec is called using `sys.exit(app.exec_()), and from what I understand, this means that when I close the Main window of the application the event scheduler is closed and the return code is passed to sys.exit that should terminate the execution of the application.

I tried to launch the application from command line in Windows, using the command python app_main.py, and what I see is that when I close the main window of the application the process seem to hang in the terminal, I never get the prompt back.
I also tried to separate the sys.exit() from the app.exec_() like this:

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    style_man = StyleManager(app)
    window = MainWindow()
    window.show()
    # sys.exit(app.exec_())
    app.exec_()
    print("I pass here!")
    sys.exit(0)

I see the print I pass here! on the terminal but also in this case I never get the prompt back. I did expect that the sys.exit() call would terminate the process, but maybe there is something that is hanging and prevents the application from closing?
Could it be the event scheduler still running? Or anything linked to the threads or to the queue?
Or should I use something different from sys.exit()?

I hope to have explained my situation and my doubts properly, if not I will try to correct my question.

After a bit of troubleshooting I think I have found the issue with my program.

For the thread I am using objects derived from QRunnable, and the threads were infinite loops, one listened to the serial, the other plotted the data when new data arrived.

Being infinite loops the thread continued running, and since everyone of them has a timer object, the timer objects likely prevented the execution scheduler to terminate.

To solve the issue I added a flag to the thread loops and used the close event of the application

    def closeEvent(self, event):
        self.set_finish_signal()
        app.exit(0)
    def set_finish_signal(self):
        self.viewWo.terminate_thread()
        self.serialWo.terminate_thread()

The run loop condition are always False when starting, and the terminate_thread function change the signal to True and the Run terminates, freeing the timer resources and allowing the execution to terminate properly.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.