简体   繁体   中英

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

This is a simplified version of the program implementing PyQt multi-threading with QObject.moveToThread. Basically, I query a webpage on a separate thread and extract the HMTL content.

I get this problem where running the code from IDLE or the Windows command line hangs python. The Windows cmd shows "QThread: Destroyed while thread is still running". However, if I run it from Pycharm, everything works fine.

You can get the .ui file here

Any ideas?

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()

I figured it out:

Both http_client and temp_thread have to be attributes or the HtmlGetter class. I think it's because otherwise python discards them when exiting the function. This is the working code:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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