pyqt多线程QThread使用moveToThread方法报错QThread: Destroyed while thread is still running解决方法

背景

在pyqt(pyside6)框架下写上位机,需要使用到多线程,用了qt的QThread。看到网上说创建一个继承自QObject类在其中写入子线程运行程序,再用moveToThread方法将运行程序移入新建的QThread中实现多线程。而我子线程运行程序是一个while死循环搭配time.sleep方法保证程序持续运行,有时关闭主窗口时会报错QThread: Destroyed while thread is still running。

class mtask(QObject):
    def __init__(self,??) -> None:
        super(heating_task,self).__init__()
        self.?? = ??

    def maintask(self):
        while(1):
            if self.flag_exit:
                break

            ...
            ...

            time.sleep(time_interval)


class MainWindow(QMainWindow):
    ...
    ...

    def ...
        self.mtask1 = mtask(??)
        self.mthread = QThread(self)
        self.mtask1.moveToThread(self.mthread)
        self.mthread.started.connect(self.mtask1.maintask)    # 连接信号槽启动线程时启动子线程运行函数(一个死循环)
        self.mthread.start()

问题根源

问题根源在time.sleep在程序结束时不会停止运行,所以有时会碰上 销毁子线程时time.sleep还在占用线程运行 的情况,虽然我延时只有0.2s,但是报错后终端会卡好几秒。

尝试解决

我重写了主窗口关闭的事件函数,按顺序将子线程运行程序设置条件停止运行了,把线程QThread也退出了(exit、quit、terminate都用过),还是时有报错QThread: Destroyed while thread is still running。我也导入thread的Event方法使用event.wait方法替代time.sleep,但还是没有解决。

最终成功解决的方法(局限)

在主窗口关闭的事件函数下,子线程运行程序停止运行后加一行time.sleep延时(同样时间),再销毁子线程(QThread.quit())。适用于子线程运行函数每次循环的延时不长的代码,延时好几秒的话,关闭主窗口就要等好几秒了。

def closeEvent(self, event: QCloseEvent) -> None:
        self.mtask1.flag_exit = 1 # 关线程运行的函数(用flag_exit作break条件终止while死循环)
        time.sleep(time_interval)   # time.sleep不能跟随程序结束而结束,立即销毁线程可能报错卡几秒
        self.mthread.quit()  # 销毁线程
        return super().closeEvent(event)    # 写def closeEvent代码提示自动生成的,应当是原本的事件函数内容,没查,看意思是调用父类的关闭事件函数

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例代码,演示如何在PyQt5中使用QThread类,并在QThread对象销毁之前停止线程以避免QThread: Destroyed while thread is still running报警。 ```python import sys import time from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, Qt from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton class MyThread(QThread): finished = pyqtSignal() message = pyqtSignal(str) def __init__(self): super().__init__() self.m_stop = False def stop(self): self.m_stop = True def run(self): while not self.m_stop: self.message.emit("Thread is running...") time.sleep(1) self.message.emit("Thread is stopped!") self.finished.emit() class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.label = QLabel(self) self.label.setGeometry(50, 50, 200, 30) self.button = QPushButton('Stop', self) self.button.setGeometry(50, 100, 100, 30) self.button.clicked.connect(self.stopThread) self.thread = MyThread() self.thread.message.connect(self.updateLabel) self.thread.finished.connect(self.threadFinished) self.thread.start() @pyqtSlot(str) def updateLabel(self, text): self.label.setText(text) def stopThread(self): self.thread.stop() @pyqtSlot() def threadFinished(self): self.thread.quit() self.thread.wait() print("Thread is finished!") self.close() if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` 在上面的示例代码中,我们定义了一个MyThread类继承自QThread,在run()函数中实现了线程的执行逻辑,并在stop()函数中发送停止信号给线程。我们还定义了一个message信号,用于在UI线程中更新标签的文本。 在主窗口类中,我们创建了一个标签和一个按钮,并将按钮的clicked信号连接到stopThread()函数。在initUI()函数中,我们创建了一个MyThread对象并启动它,并将message信号连接到updateLabel()函数,将finished信号连接到threadFinished()函数。 在stopThread()函数中,我们发送停止信号给线程。在threadFinished()函数中,我们先使用quit()函数停止线程的事件循环,然后使用wait()函数等待线程完成,并在完成后关闭主窗口。 这样,我们就能够在QThread对象销毁之前正确地停止线程,避免了QThread: Destroyed while thread is still running报警。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值