如何从 QT 窗口内停止多进程?
Posted
技术标签:
【中文标题】如何从 QT 窗口内停止多进程?【英文标题】:How could I stop a multiprocess from inside a QT window? 【发布时间】:2017-04-26 14:49:44 【问题描述】:我正在尝试在我的程序中使用一些多处理,但我遇到了一个问题。 通过按下 Qt 窗口中的按钮,我启动了一个多处理,这使得这里不重要的东西。我无法通过其他按钮阻止它。到目前为止,我已经得到了这个代码:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
import time
import multiprocessing
def mp_worker(a):
for i in range(a):
time.sleep(1)
print('keep going :' +str(i))
return
def Multifunc(k):
for i in range(k):
p = multiprocessing.Process(target= mp_worker , args=(10,))
p.start()
class SurfViewer(QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
self.parent = parent
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.mainHBOX_param_scene = QHBoxLayout()
self.Param_box = QGroupBox(" ")
self.layout_Param_box = QVBoxLayout()
self.Button_Threads = QPushButton('Run Threads')
self.Button_stop = QPushButton('stop Threads')
self.layout_Param_box.addWidget(self.Button_Threads)
self.layout_Param_box.addWidget(self.Button_stop)
self.Param_box.setLayout(self.layout_Param_box)
self.mainHBOX_param_scene.addWidget(self.Param_box)
self.centralWidget.setLayout(self.mainHBOX_param_scene)
self.Button_Threads.clicked.connect(self.RunThreads)
self.Button_stop.clicked.connect(self.stop)
def RunThreads(self):
Multifunc(10)
return
def stop(self):
# How stop the multi process ?
return
def main():
app = QApplication(sys.argv)
ex = SurfViewer(app)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
所以RunThreads
函数运行多处理,stop
函数应该停止多处理,但由于 python 在我的工作人员内部,我无法从 Qt 类停止多处理。我不明白我的stop
function 应该是什么。我应该使用某种信号来执行多处理停止吗?或者我应该将一些变量传递给Multifunc
?我对此有点困惑。
【问题讨论】:
【参考方案1】:为了停止进程,您必须拥有 Process 对象,我们将对它们进行以下修改:
-
我们将返回进程列表
def Multifunc(k):
ps = []
for i in range(k):
p = multiprocessing.Process(target= mp_worker , args=(10,))
ps.append(p)
p.start()
return ps
-
我们将对象列表保存在类的属性中。
def RunThreads(self):
self.ps = Multifunc(10)
return
-
我们为每个进程执行终止函数:
def stop(self):
# How stop the multi process ?
for p in self.ps:
p.terminate()
完整代码:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
import time
import multiprocessing
def mp_worker(a):
for i in range(a):
time.sleep(1)
print('keep going :' +str(i))
return
def Multifunc(k):
ps = []
for i in range(k):
p = multiprocessing.Process(target= mp_worker , args=(10,))
ps.append(p)
p.start()
return ps
class SurfViewer(QMainWindow):
def __init__(self, parent=None):
super(SurfViewer, self).__init__()
self.parent = parent
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.mainHBOX_param_scene = QHBoxLayout()
self.Param_box = QGroupBox(" ")
self.layout_Param_box = QVBoxLayout()
self.Button_Threads = QPushButton('Run Threads')
self.Button_stop = QPushButton('stop Threads')
self.layout_Param_box.addWidget(self.Button_Threads)
self.layout_Param_box.addWidget(self.Button_stop)
self.Param_box.setLayout(self.layout_Param_box)
self.mainHBOX_param_scene.addWidget(self.Param_box)
self.centralWidget.setLayout(self.mainHBOX_param_scene)
self.Button_Threads.clicked.connect(self.RunThreads)
self.Button_stop.clicked.connect(self.stop)
def RunThreads(self):
self.ps = Multifunc(10)
return
def stop(self):
# How stop the multi process ?
for p in self.ps:
p.terminate()
def main():
app = QApplication(sys.argv)
ex = SurfViewer(app)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
【讨论】:
英俊。现在我有一个解决方案似乎很明显:) 谢谢。以上是关于如何从 QT 窗口内停止多进程?的主要内容,如果未能解决你的问题,请参考以下文章