2013-03-28 155 views
1

我有一個使用QThreads和信號/插槽與GUI進行通信的程序。 它具有下面顯示的簡化形式。QThread信號/插槽到QProcesses

但是,我想將其更改爲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_()) 

回答