这是使用QObject.moveToThread实现PyQt多线程的程序的简化版本.基本上,我在一个单独的线程上查询网页并提取HMTL内容.
我在从IDLE或Windows命令行运行代码挂起python时遇到此问题. Windows cmd显示“ QThread:线程仍在运行时被销毁”.但是,如果我从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类.我认为是因为否则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()