2017-10-17 163 views
0

我有一个PYSide2主机,当按钮单击时,我创建一个进程名称TTT,我想当我关闭大型机时,进程也关闭,但事实并非如此。当我关闭PYQT MainFrame时,如何关闭多处理器

我该怎么办?

class Test7(QMainWindow): 

    def __init__(self): 
     QMainWindow.__init__(self) 
     self.setupUi() 

    def setupUi(self): 
     ...(not important code here)... 
     self.pushButton.clicked.connect(self.btnClicked) 


    def btnClicked(self): 
     ttt = TTT('aaa') 
     ttt.deman = False 
     ttt.start() 


class TTT(multiprocessing.Process): 
    def __init__(self, name): 
     multiprocessing.Process.__init__(self) 
     print('nothing to do') 

    def run(self): 
     while True: 
      print('abc') 
      time.sleep(10) 

if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    w = Test7() 
    w.show() 
    sys.exit(app.exec_()) 

回答

0

您可以将daemon设置为True

进程的守护进程标志,一个布尔值。在调用start()之前,必须设置为 。

初始值从创建过程继承。

当进程退出时,它将尝试终止其子进程的所有守护进程 子进程。

请注意,不允许守护进程创建子进程。 否则,如果一个守护进程在其父进程退出时终止,则其子进程将离开子进程。另外,这些不是Unix守护进程或服务,它们是正常的进程,如果非守护进程退出,它将终止(并且未加入) 。

把你的代码片段为例:

class TTT(multiprocessing.Process): 
    def __init__(self, name): 
     multiprocessing.Process.__init__(self) 
     self.daemon = True 
     print('nothing to do') 

    def run(self): 
     while True: 
      print('abc') 
      time.sleep(10)