QThread在主循环死后仍在运行

问题描述 投票:0回答:1

我正在努力研究必须计数Outlook X中每隔x分钟更新一次电子邮件的数量。

我创建了一个QThread来完成此任务,同时冻结我的GUI。

一切正常,但是当我退出软件时,线程仍在运行,原因是我使用了无限循环,以便它可以永久更新数量。

我通过声明一个全局变量和一个局部变量并通过信号递增来找到了一个解决方案。我的while循环比较它们,如果可以的话继续。

由于Qthread没有isDaemon方法,所以我不确定该线程是否真的死了,并且不再只是不再执行可见任务。

以下是我的代码的一个示例:

continuation = 1类App_Direction(QtWidgets.QWidget):    def __init__(self):
        super(App_Direction,self).__init__()
        self.setWindowTitle("test")   
        self.resize(1000,1050)
        self.move(1500,100)
        self.setStyleSheet("font: 10pt;")
        self.feed_stats_label()

    @QtCore.pyqtSlot(int)    
    def feed_stats_label(self):
        self.workthread=WorkerThread()
        self.workthread.mail_sig.connect(self.modif)
        self.workthread.continuation_sig.connect(self.continuation)
        self.workthread.start()

    @QtCore.pyqtSlot(list)
    def modif (self,liste=()):
        print("Je ne recois plus le signal, car je suis mort")

    @QtCore.pyqtSlot(list)
    def continuation(self, arg):
        global continuation
        continuation+=1

class WorkerThread(QtCore.QThread):
    mail_sig = QtCore.pyqtSignal(list) 
    continuation_sig=QtCore.pyqtSignal(list)

    def __init__(self, parent=None):
        super().__init__(parent)         
    def run(self):   


        self.continuation=1
        global continuation
        while self.continuation==continuation:
        print("Et ca continue encore et encore")
        self.mail_sig.emit([]) # envoi du signal de progression d'exécution
        self.continuation+=1
        self.continuation_sig.emit([self.continuation])
        time.sleep(10) 
if __name__ == '__main__':
    QtWidgets.QApplication.setStyle("Fusion")   
    app=QtWidgets.QApplication ([])
    win= App_Direction()
    win.show()      
    app.exec_()<code>
python multithreading pyqt pyqt5 qthread
1个回答
0
投票

虽然QThread没有守护程序标志,但它的作用确实类似于守护程序。考虑删除连续变量(和一些重构)的示例:

from PyQt5 import QtWidgets, QtCore
import time


class AppDirection(QtWidgets.QWidget):

    def __init__(self):
        super(AppDirection, self).__init__()
        self.setWindowTitle("test")
        self.resize(800, 600)
        self.setStyleSheet("font: 10pt;")
        self.work_thread = None
        self.feed_stats_label()

    @QtCore.pyqtSlot(int)
    def feed_stats_label(self):
        self.work_thread = WorkerThread()
        self.work_thread.mail_sig.connect(self.modif)
        self.work_thread.start()

    @QtCore.pyqtSlot()
    def modif(self):
        print("I am receiving the signal")


class WorkerThread(QtCore.QThread):
    mail_sig = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)

    def run(self):
        while True:
            print("I am sending the signal.")
            self.mail_sig.emit()
            time.sleep(0.5)


if __name__ == '__main__':
    QtWidgets.QApplication.setStyle("Fusion")
    app = QtWidgets.QApplication([])
    win = AppDirection()
    win.show()
    app.exec_()

关闭窗口时,该线程也停止打印。

© www.soinside.com 2019 - 2024. All rights reserved.