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代码提示自动生成的,应当是原本的事件函数内容,没查,看意思是调用父类的关闭事件函数

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值