【发布时间】:2013-03-28 15:22:44
【问题描述】:
我有一个使用 QThreads 和 Signals/Slots 与 GUI 通信的程序。 它具有如下所示的简化形式。
但是,我想将其更改为 QProcess,以便我可以利用多核处理。有没有一种简单的方法可以做到这一点?
如果我只是将 QThread 更改为 QProcess ,则进程没有 moveToThread() 函数。我一直在尝试几种不同的多核处理方法,例如multiprocessing 模块中的Pipes() 和Queues(),但我无法让任何东西很好地工作。所以,我认为使用 QProcess 会更容易,因为我已经在 Qtland。
这是我为QThreads、信号和插槽提供的简化代码。
from PyQt4 import QtCore, QtGui
import multiprocessing as mp
import numpy as np
import sys
class Spectra(QtCore.QObject):
update_signal = QtCore.pyqtSignal(str)
done_signal = QtCore.pyqtSignal()
def __init__(self, spectra_name, X, Y):
QtCore.QObject.__init__(self)
self.spectra_name = spectra_name
self.X = X
self.Y = Y
self.iteration = 0
@QtCore.pyqtSlot()
def complex_processing_on_spectra(self):
for i in range(0,99999):
self.iteration += 1
self.update_signal.emit(str(self.iteration))
self.done_signal.emit()
class Spectra_Tab(QtGui.QTabWidget):
start_comp = QtCore.pyqtSignal()
kill_thread = QtCore.pyqtSignal()
def __init__(self, parent, spectra):
self.parent = parent
self.spectra = spectra
QtGui.QTabWidget.__init__(self, parent)
self.treeWidget = QtGui.QTreeWidget(self)
self.properties = QtGui.QTreeWidgetItem(self.treeWidget, ["Properties"])
self.step = QtGui.QTreeWidgetItem(self.properties, ["Iteration #"])
thread = QtCore.QThread(parent=self)
self.worker = self.spectra
self.worker.moveToThread(thread)
self.worker.update_signal.connect(self.update_GUI)
self.worker.done_signal.connect(self.closeEvent)
self.start_comp.connect(self.worker.complex_processing_on_spectra)
self.kill_thread.connect(thread.quit)
thread.start()
@QtCore.pyqtSlot(str)
def update_GUI(self, iteration):
self.step.setText(0, iteration)
def start_computation(self):
self.start_comp.emit()
def closeEvent(self):
print 'done with processing'
self.kill_thread.emit()
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self)
self.setTabShape(QtGui.QTabWidget.Rounded)
self.centralwidget = QtGui.QWidget(self)
self.top_level_layout = QtGui.QGridLayout(self.centralwidget)
self.tabWidget = QtGui.QTabWidget(self.centralwidget)
self.top_level_layout.addWidget(self.tabWidget, 1, 0, 25, 25)
process_button = QtGui.QPushButton("Process")
self.top_level_layout.addWidget(process_button, 0, 1)
QtCore.QObject.connect(process_button, QtCore.SIGNAL("clicked()"), self.process)
self.setCentralWidget(self.centralwidget)
self.centralwidget.setLayout(self.top_level_layout)
# Open several files in loop from button - simplifed to one here
X = np.arange(0.1200,.2)
Y = np.arange(0.1200,.2)
self.spectra = Spectra('name', X, Y)
self.spectra_tab = Spectra_Tab(self.tabWidget, self.spectra)
self.tabWidget.addTab(self.spectra_tab, 'name')
def process(self):
self.spectra_tab.start_computation()
return
if __name__ == "__main__":
app = QtGui.QApplication([])
win = MainWindow()
win.show()
sys.exit(app.exec_())
【问题讨论】:
标签: python qt user-interface multiprocessing