PySide2 Qthread 崩溃 [英] PySide2 Qthread crash
问题描述
由于Qtcore.Signal,我想使用PySide2 Qtcore.Qthread,但最终出现此错误:进程完成,退出代码 -1073740791
I want to use PySide2 Qtcore.Qthread because of Qtcore.Signal, but I end up with this error: Process finished with exit code -1073740791
from PySide2.QtCore import QThread
class Thread(QThread):
def run(self):
print('task started')
k = 0
for i in range(10000):
for j in range(5000):
k += 1
print('task finished')
Thread().start()
希望有这些打印,但我有这个错误:
expect to have those prints but I have this error:
进程已完成,退出代码为 -1073740791
Process finished with exit code -1073740791
更新:
那么,为什么这段代码也会抛出同样的错误?
so, why this code also throw the same error?
class Thread(QThread):
done = Signal()
def __init__(self):
super(Thread, self).__init__()
def run(self):
print('task started')
k = 0
for i in range(10000):
for j in range(5000):
k += 1
print('task finished')
self.done.emit()
class Widget(QtWidgets.QWidget):
def __init__(self):
super(Widget, self).__init__()
btn = QtWidgets.QPushButton('test', parent=self)
btn.clicked.connect(self.clicked)
btn.show()
def clicked(self):
t = Thread()
t.done.connect(self.done)
t.start()
def done(self):
print('done')
app = QtWidgets.QApplication()
window = Widget()
window.show()
sys.exit(app.exec_())
推荐答案
说明
如果您在 CMD/终端中运行您的代码,您将收到以下错误:
Explanation
If you run your code in a CMD/Terminal you will get the following error:
QThread: Destroyed while thread is still running
Aborted (core dumped)
而且这个错误是因为线程在运行时被销毁了,因为它是一个局部变量,另一方面QThread需要一个事件循环来运行
And the error is caused because the thread is destroyed while it is still running since it is a local variable, on the other hand QThread needs an event loop to run
import sys
from PySide2.QtCore import QCoreApplication, QThread
class Thread(QThread):
def run(self):
print("task started")
k = 0
for i in range(10000):
for j in range(5000):
k += 1
print("task finished")
if __name__ == "__main__":
# create event loop
app = QCoreApplication(sys.argv)
th = Thread()
th.start()
th.finished.connect(QCoreApplication.quit)
sys.exit(app.exec_())
更新:
"t" 是一个局部变量,在执行 clicked 后将被消除,导致与您的初始代码相同的问题,解决方案是防止它立即被销毁,为此有两个选项:
"t" is a local variable that will be eliminated after executing clicked causing the same problem as your initial code, the solution is to prevent it from being destroyed instantly and for this there are 2 options:
- 制作t"类属性
def clicked(self):
self.t = Thread()
self.t.done.connect(self.done)
self.t.start()
- 将 QThread 存储在具有更长生命周期的容器中:
class Widget(QtWidgets.QWidget):
def __init__(self):
super(Widget, self).__init__()
btn = QtWidgets.QPushButton('test', parent=self)
btn.clicked.connect(self.clicked)
self.container = []
def clicked(self):
t = Thread()
t.done.connect(self.done)
t.start()
self.container.append(t)
# ...
- 将它作为父级传递给self",但为此,Thread 允许接收是必要的,因此您必须在构造函数中实现它:
class Thread(QThread):
done = Signal()
def __init__(self, parent=None):
super(Thread, self).__init__(parent)
# ...
def clicked(self):
t = Thread(self)
t.done.connect(self.done)
t.start()
这篇关于PySide2 Qthread 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!