我正在尝试在我的程序中使用一些多处理,而我正面临一个问题。 我通过在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在我的worker中,我无法停止Qt类的多处理。我不明白我的stop
功能应该是什么。我应该使用SIGNAL来执行多处理停止吗?或者我应该将一些变量传递给Multifunc
?我对此有点困惑。
答案 0 :(得分: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()