简体   繁体   English

QThread:当线程仍在Python中运行时被破坏

[英]QThread: Destroyed while thread is still running in Python

I've got this part of code, which sometimes works and sometimes throws warning: 我有这部分代码,有时可以工作,有时会发出警告:
QThread: Destroyed while thread is still running

This part is in UiMainWindow class 这部分在UiMainWindow类中

obj_thread = QtCore.QThread()

def ok():
    self.module_src_tree.setStyleSheet('background-color: #81F781')
    obj_thread.quit()

def err():
   self.module_src_tree.setStyleSheet('background-color: #F78181')
   obj_thread.quit()

tmp = self.Temp(self, module_revision)
tmp.moveToThread(obj_thread)
tmp.finished.connect(ok)
tmp.error.connect(err)
obj_thread.started.connect(tmp.run)
obj_thread.start()

This is class in UiMainWindow class 这是UiMainWindow类中的类

class Temp(QtCore.QObject):
    finished = QtCore.pyqtSignal()
    error = QtCore.pyqtSignal()

    def __init__(self, gui, module_revision):
        QtCore.QObject.__init__(self)
        self.gui = gui
        self.module_revision = module_revision

    def run(self):
        try:
            self.gui.dp.pack_module_source(self.gui.module_src_show_list, self.gui.module_src_pack_list,
                                           path=str(self.gui.path_box.text()), revision=self.module_revision)
            self.finished.emit()
        except Exception as e:
            self.error.emit()
            raise e

What I am trying to do with this code - I want to zip some files without freezing main application. 我想用此代码做什么-我想压缩一些文件而不冻结主应用程序。 So I start new Thread which works in background. 所以我开始在后台工作的新线程。 But I need functionality that a Widget changes it's color after the zipping is done to green or to red if something went wrong. 但是我需要的功能是,在完成压缩后,小部件可以将其颜色更改为绿色或红色(如果出了问题)。
Maybe I am doing something wrong? 也许我做错了什么? Maybe that is not the way? 也许那不是办法吗?
Most probles I've got with changing color part. 我最常遇到的问题是要改变颜色。

Best Regards, 最好的祝福,
Marek 马雷克


@three_pineapples, seeems like it doesn't start at all sometimes. @three_pineapples,似乎好像有时根本没有开始。 But there is no error/warning right now. 但是现在没有错误/警告。
I have modified code to this: 我已经对此进行了修改:

class UiMainWindow(object):
    # thread pool
    thread_pool = [None, None, None, None]

This is before class constructor. 这是在类构造函数之前。 Temp class stays the same as above, and Thread calling part of code looks like this right now: Temp类保持与上面相同,并且Thread调用代码的一部分现在看起来像这样:

self.thread_pool[3] = None
self.thread_pool[3] = QtCore.QThread()

def ok():
    self.module_src_tree.setStyleSheet('background-color: #81F781')
    self.thread_pool[3].quit()

def err():
    self.module_src_tree.setStyleSheet('background-color: #F78181')
    self.thread_pool[3].quit()

tmp = self.Temp(self, module_revision)
tmp.moveToThread(self.thread_pool[3])
tmp.finished.connect(ok)
tmp.error.connect(err)
self.thread_pool[3].started.connect(tmp.run)
self.thread_pool[3].start()

This issue occurs when the thread is garbage collected by Python. 当线程被Python垃圾回收时,会发生此问题。

You need to save a reference to your QThread so it is not garbage collected. 您需要保存对您的QThread的引用,以便不会对其进行垃圾回收。 Just do self.obj_thread = QtCore.QThread() 只是做self.obj_thread = QtCore.QThread()

If there is the possibility that multiple QThread s will exist at once, and you are storing the references in the same variable, then you probably need to store the objects in a list instead. 如果有可能同时存在多个QThread ,并且您将引用存储在同一变量中,那么您可能需要将对象存储在列表中。 However, you will need to clear out objects from the list (so they are garbage collected) when a given thread has finished so that you don't introduce a memory leak to your application. 但是,当给定线程完成时,您将需要从列表中清除对象(以便对它们进行垃圾收集),以免对应用程序造成内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PyQt QThread:在线程仍在运行时被销毁 - PyQt QThread: Destroyed while thread is still running 退出时“ QThread:线程仍在运行时被销毁” - 'QThread: Destroyed while thread is still running' on quit QThread:在线程仍在运行时被销毁 - QThread: Destroyed while thread is still running PyQt5 - QThread:线程仍在运行时被销毁 - PyQt5 - QThread: Destroyed while thread is still running 使用Python的QtWebkit呈现基于Javascript的页面,获取QThread:在线程仍在运行时被销毁 - Using Python's QtWebkit to render Javascript-based page, get QThread: Destroyed while thread is still running “QThread:当线程仍在运行时被破坏”从Windows cmd或IDLE运行但不是从PyCharm运行? - “QThread: Destroyed while thread is still running” when run from the Windows cmd or IDLE but not from PyCharm? PYQt5:QThread:线程仍在运行时销毁(实例分配为 QDialog ) - PYQt5: QThread: Destroyed while thread is still running ( with instance assigned as QDialog ) QThread:在线程仍在运行时被销毁:如何正确使用 QThreads 和 QTimers? - QThread: Destroyed while thread is still running: How to proper use QThreads with QTimers? PyQt5:在线程仍在运行时销毁 - PyQt5 : Destroyed while thread is still running Python:线程是否仍在运行 - Python: is thread still running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM