我有一个python GUI,它通过套接字从C++应用程序接收数据,并在Qthread中更新其字段(Qt网格对象)和3D模型。更新字段在线程中工作正常,但是当我添加函数以将 3D 模型(使用更新字段中的值)更新到线程中时,我收到"致命的 Python 错误:GC 对象已被跟踪"。代码运行几毫秒,但随后崩溃并出现错误。3D模型创建和转换基于Mayavi.mlab包。
我还尝试将更新 3D 模型功能放在另一个线程中,但这并没有消除错误。为什么会发生此错误/如何摆脱它?
主代码:
self.PSRThread = 0 # thread for the PSR server communication
self.PSRUpdateThread = QtCore.QThread() # thread for updating the PSR fields
self.PSRUpdateThread.start()
self.PSRComm = 0
def InitPSRComm(self): # activates on button click
if (self.PSRThread == 0):
self.PSRThread = ServerPSR.StartThread()
self.PSRComm = UpdatePSRFields.PSRFieldUpdater( self.UpdateJointFields, self.PSRThread.receiveJointData,
self.PSRThread.sendJointData, self.MovePSRModel, self.GetJoints,
self.PSRTargetJointFields )
self.PSRComm.sending = True
self.PSRComm.moveToThread(self.PSRUpdateThread)
self.PSRComm.start.emit("Start")
print "Update PSR Fields thread started"
if (self.PSRComm.sending == False):
self.PSRComm.sending = True
线程代码:
class PSRFieldUpdater(QtCore.QObject):
start = QtCore.Signal(str)
def __init__(self, UpdateFieldsFunc, PSRThreadRecvFunc, PSRThreadSendFunc, UpdatePSRModelFunc, GetJointsFunc,
*args):
super(PSRFieldUpdater, self).__init__()
self.UpdateFieldsFunc = UpdateFieldsFunc
self.PSRThreadRecvFunc = PSRThreadRecvFunc
self.PSRThreadSendFunc = PSRThreadSendFunc
self.UpdatePSRModelFunc = UpdatePSRModelFunc
self.GetJointsFunc = GetJointsFunc
self.args = args
self.start.connect(self.run)
self.sending = False
def run(self):
while (True):
#print "sending status is: ", self.sending
if (self.sending):
PSRjoints = self.GetJointsFunc(*self.args)
self.PSRThreadSendFunc(PSRjoints)
#print "PSRThreadSendFunc called"
self.sending = False
j = self.PSRThreadRecvFunc()
self.UpdateFieldsFunc(j)
print "PSR fields updated"
self.UpdatePSRModelFunc()
MovePSRModel():
def MovePSRModel(self):
PSRjoints = self.GetJoints(self.PSRJointFields)
j = transformPSR.PSRJoints(PSRjoints, partsList)
j.MovePSR(PSRjoints, partsList)
如果需要任何其他信息,请告诉我。
因此,当您尝试在主线程中使用线程时,事实证明Qt GUi会发生奇怪的事情(我发现此链接是相关的)。我在主 GUI 线程中实现了东西,并使用信号与之通信以进行更新。