"QThread: Destroyed while thread is still running" 当 运行 来自 Windows cmd 或 IDLE 而不是来自 PyCharm 时?

"QThread: Destroyed while thread is still running" when run from the Windows cmd or IDLE but not from PyCharm?

这是用QObject.moveToThread实现PyQt多线程程序的简化版。基本上,我在单独的线程上查询网页并提取 HMTL 内容。

我遇到这个问题,运行从 IDLE 或 Windows 命令行中调用代码挂起 python。 Windows 命令显示 "QThread: Destroyed while thread is still running"。但是,如果我 运行 它来自 Pycharm,一切正常。

您可以获得.ui文件here

有什么想法吗?

import requests
import sys
from PyQt4 import QtGui, uic
from PyQt4.QtCore import QObject, pyqtSlot, pyqtSignal, QThread


qtCreatorFile = "window.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)


class HttpClient(QObject):

    finished =  pyqtSignal(str)

    def __init__(self):
        QObject.__init__(self)

    @pyqtSlot()
    def retrieve_page(self, url):
        response = requests.get(url)
        self.finished.emit(response.text)


class HtmlGetter(QtGui.QMainWindow, Ui_MainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)       
        self.go_button.clicked.connect(self.query_page)

    def query_page(self):
        http_client = HttpClient()
        temp_thread = QThread()
        http_client.moveToThread(temp_thread)

        temp_thread.started.connect(
        lambda: http_client.retrieve_page("http://www.google.com/"))
        http_client.finished.connect(self.show_html)

        # Terminating thread gracefully.
        http_client.finished.connect(temp_thread.quit)
        http_client.finished.connect(http_client.deleteLater)
        temp_thread.finished.connect(temp_thread.deleteLater)

        temp_thread.start()

    def show_html(self, html_text):
        print(html_text)


def main():
    app = QtGui.QApplication(sys.argv)
    window = HtmlGetter()
    window.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

我想通了:

http_client 和 temp_thread 都必须是属性或 HtmlGetter class。我认为这是因为否则 python 在退出函数时会丢弃它们。这是工作代码:

import requests
import sys
from PyQt4 import QtGui, uic
from PyQt4.QtCore import QObject, pyqtSlot, pyqtSignal, QThread


qtCreatorFile = "window.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)


class HttpClient(QObject):

    finished =  pyqtSignal()
    send_text = pyqtSignal(str)

    def __init__(self):
        QObject.__init__(self)

    @pyqtSlot()
    def retrieve_page(self, url):
        response = requests.get(url)
        self.send_text.emit(response.text)
        self.finished.emit()


class HtmlGetter(QtGui.QMainWindow, Ui_MainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)       
        self.go_button.clicked.connect(self.query_page)

    def query_page(self):
        self.http_client = HttpClient()
        self.temp_thread = QThread()
        self.http_client.moveToThread(self.temp_thread)

        self.temp_thread.started.connect(
        lambda: self.http_client.retrieve_page("http://www.google.com/"))
        self.http_client.send_text.connect(self.show_html)

        # Terminating thread gracefully.
        self.http_client.finished.connect(self.temp_thread.quit)
        self.http_client.finished.connect(self.http_client.deleteLater)
        self.temp_thread.finished.connect(self.temp_thread.deleteLater)

        self.temp_thread.start()

    def show_html(self, html_text):
        print(html_text)


def main():
    app = QtGui.QApplication(sys.argv)
    window = HtmlGetter()
    window.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()