当我关闭PYQT MainFram时,如何关闭多处理

2024-04-20 15:25:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个PYSide2主机,当我点击按钮创建一个进程名TTT时, 我想当我关闭主机时,进程也会关闭,但事实并非如此

我该怎么办

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_())

Tags: nameself进程initdefmultiprocessingprocessclass
1条回答
网友
1楼 · 发布于 2024-04-20 15:25:14

您可以将daemon设置为True

The process’s daemon flag, a Boolean value. This must be set before start() is called.

The initial value is inherited from the creating process.

When a process exits, it attempts to terminate all of its daemonic child processes.

Note that a daemonic process is not allowed to create child processes. Otherwise a daemonic process would leave its children orphaned if it gets terminated when its parent process exits. Additionally, these are not Unix daemons or services, they are normal processes that will be terminated (and not joined) if non-daemonic processes have exited.

以您的代码段为例:

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)

相关问题 更多 >