简体   繁体   English

pyqt:如何正确退出线程

[英]pyqt: How to quit a thread properly

I wrote an pyqt gui and used threading to run code which needs a long time to be executed, but I want to have the choice to stop the execution safely. 我写了一个pyqt gui并使用线程来运行需要很长时间才能执行的代码,但是我想选择安全地停止执行。 I dont want to use the get_thread.terminate() method. 我不想使用get_thread.terminate()方法。 I want to stop the code by a special function (maybe del ()). 我想通过特殊功能(也许是del ())停止代码。 My problem is that, I wrote the code in a own class and just want to abort the class without changing a lot of syntax. 我的问题是,我在自己的类中编写了代码,只是想在不更改大量语法的情况下中止该类。

Edit: It was mentioned that one has to pass a flag to the class, which has to be checked constantly. 编辑:有人提到必须将一个标志传递给该类,该标志必须不断检查。 How do I send this flag to the class? 如何将此标志发送给班级? Because the flag has to change the value, when one presses the stop button. 因为标志必须更改值,所以当按下停止按钮时。

Edit 2: My solution so far is, to declerate a global variable with the name running_global. 编辑2:到目前为止,我的解决方案是派生一个名称为running_global的全局变量。 I changed self.get_thread.terminate() to running_global = False and I check constantly in my long_running_prog if the variable has been set False. 我将self.get_thread.terminate()更改为running_global = False,并在long_running_prog中不断检查变量是否设置为False。 I think this solution is ugly, so I would be pretty happy if someone has a better idea. 我认为这种解决方案很难看,所以如果有人有更好的主意,我会很高兴。

This is my code for the dialog where I start the thread: 这是我在其中启动线程的对话框的代码:

class SomeDialog(QtGui.QDialog,
     userinterface_status.Ui_status_window):
     finished = QtCore.pyqtSignal(bool)

    def __init__(self):
        """
        :param raster: Coordinates which are going to be scanned.
        """
        super(self.__class__, self).__init__()  # old version, used in python 2.
        self.setupUi(self)  # It sets up layout and widgets that are defined
        self.get_thread = SomeThread()

        # Conencting the buttons
        self.start_button.clicked.connect(self.start)
        self.stop_button.clicked.connect(self.stop)
        self.close_button.clicked.connect(self.return_main)

        # Connecting other signals
        self.connect(self.get_thread, QtCore.SIGNAL("stop()"), self.stop)
        self.connect(self.get_thread, QtCore.SIGNAL("update_status_bar()"), self.update_status_bar)

    def return_main(self):
        """
        Function is excecuted, when close button is clicked.
        """
        print("return main")
        self.get_thread.terminate()
        self.close()

    def start(self):
        """
        Starts the thread, which means that the run method of the thread is started.
        """
        self.start_button.setEnabled(False)
        self.get_thread.start()

    def stop(self):
        print("Stop programm.")
        self.start_button.setEnabled(True)
        self.get_thread.quit()

    def end(self):
        QtGui.QMessageBox.information(self, "Done!", "Programm finished")



    def closeEvent(self, event):
        """
        This method is called, when the window is closed and will send a signal to the main window to activaete the
        window again.
        :param event:
        """
        self.finished.emit(True)
        # close window
        event.accept()

In the following class is the code for the thread: 在以下类中是线程的代码:

class SomeThread(QtCore.QThread):
    finished = QtCore.pyqtSignal(bool)

    def __init__(self):
        QtCore.QThread.__init__(self)

    def __del__(self):
        print("del")
        self.wait()

    def run(self):
        self.prog = long_running_prog(self.emit)  # Sending from the prog signals
        self.prog.run()
        self.prog.closeSystem()  # Leaving the programm in a safe way.

So if one presses the stop button, the programm should instantly shut down in a save way. 因此,如果按下停止按钮,则该程序应立即以保存方式关闭。 Is there a way to abort the class in a save way? 有没有办法以保存的方式中止课程? For example can I pass a variable to the long_running_prog class which turns True, when one presses the stop button? 例如,当有人按下停止按钮时,我可以将变量传递给long_running_prog类,该类变为True吗? If somethin like this is possible, could one tell me how? 如果有这种可能,请告诉我如何做?

Thanks for your help in advance 谢谢您的帮助

I hope you understand my problem. 希望你能理解我的问题。 Greetings Hizzy 问候头晕

This is impossible to do unless prog.run(self) would periodically inspect a value of a flag to break out of its loop. 除非prog.run(self)会定期检查标志的值以脱离其循环,否则这是不可能的。 Once you implement it, __del__(self) on the thread should set the flag and only then wait . 一旦实现它,线程上的__del__(self)应该设置该标志,然后再wait

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM