我试图理解这个How to use QThread correctly in pyqt with moveToThread()?中的代码
该部分具有:
mainwin.__init__ : MainThread, 140221684574016,
GenericWorker.__init__ : MainThread, 140221684574016,
GenericWorker.run : Dummy-1, 140221265458944,
mainwin.addBatch : Dummy-1, 140221265458944,
mainwin.add : Dummy-1, 140221265458944,
mainwin.add : Dummy-1, 140221265458944,
mainwin.add : Dummy-1, 140221265458944,
mainwin.add : Dummy-1, 140221265458944,
mainwin.add : Dummy-1, 140221265458944,
mainwin.add : Dummy-1, 140221265458944
我感兴趣的是,Dummy-1元素到底是什么,我的理解是这些是工作线程在执行任务,但是它们会永远“活着”吗?或者他们会被自己收集垃圾。如果add batch执行了10k次,那么代码输出是否会有10k个Dummy-1项? 我之所以这么问,是因为我在使用子类Qthreads时看到了类似的输出(在Eclispe中使用Pydev,而在调试模式下运行代码)。在
我不确定这是否意味着某种最终会消耗大量资源的泄漏(线程泄漏)。在
QThread不是Qt的一个线程,而是一个类,用于处理每个操作系统的本机线程python的线程模块,这在docs中提到:
因此,当使用
threading.current_thread()
时,python试图获取本机线程,但是正如docs所指出的那样(您也可以在source code中看到它):因为QThread创建了一个线程模块不处理的线程,这将返回一个表示该线程的虚拟线程。在
它们存储在线程模块内的字典中,因此不会使用GC删除它们,也不会导致泄漏。这在docs中提到:
{和}中的}:
总之,这些虚构的元素提供了线程未处理的线程的信息,这些元素不受GC的影响,因为它们存储在字典中,因此不是内存泄漏。在
相关问题 更多 >
编程相关推荐