gpt4 book ai didi

python - 关闭 PYQT MainFrame 时如何关闭多处理

转载 作者:太空宇宙 更新时间:2023-11-03 14:37:53 25 4
gpt4 key购买 nike

我有一个 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_())

最佳答案

您可以将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)

关于python - 关闭 PYQT MainFrame 时如何关闭多处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46785153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com