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