我有一个程序,它使用QThreads和信号/插槽来与图形用户界面通信。它具有如下所示的简化形式。
但是,我想将其更改为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_())发布于 2013-03-29 00:54:32
进程没有moveToThread(),因为进程位于自己的内存空间中,所以它看不到MainWindow或它的任何成员。开始使用QProcess的应用程序应该能够作为独立应用程序执行。
要在另一个QProcess中运行spectra,您需要使spectra成为一个单独的可执行模块,而不是像现在这样作为MainWindow成员。
编辑:
您需要定义对MainWindow依赖性最小的自包含模块-它可以是仅光谱过程,也可以是带有选项卡的光谱过程。您可以在构造时或通过标准输入向process传递信息,并通过标准输出从process检索数据。在选择律师来放置进程时,关键思想是最小化进程和MainWindow之间的通信和依赖。你可以把一个进程想象成一个简单的C程序:
int main(int argc,char* argv[]);
您可以在启动时传递参数,如果需要,可以通过cin/stdin从MainWindow获得额外的输入,通过cout/stdout/stderr将一些结果输出到MainWindow (QProcess有相应的接口)。
https://stackoverflow.com/questions/15685695
复制相似问题