如何通过pyqt4中的多线程进行计算密集型操作

时间:2019-03-31 09:26:42

标签: python-3.x multithreading pyqt4

我正在编写一个GUI,该GUI从hdf5文件中读取3D numpy数组,然后对数组进行一些繁重的数学运算,然后将计算出的数组输出到hdf5中,有多个单选按钮可供我们选择数学要完成的操作。问题是,当我运行繁重的数学功能时,整个系统冻结,并且GUI变得无响应。

我还有一个按钮来启动Mayavi显示窗口以显示音量,该窗口也冻结了GUI。

可以在pyqt中的其他线程中完成此操作,以使系统不会冻结并且GUI保持响应状态。

我尝试了几种解决方案

PyQt4 multithreading using QThread

call function of main thread from secondary thread

但是我没有得到任何答案

from PyQt4 import QtGui
import sys
import Sax_in_PyQt4 as design 
from PyQt4 import QtCore
import util
import CompleTrace
import h5py
import dask.array as da
import numpy as np
import scipy
from SignalProcess import SignalProcess as sp 
import urllib


class ComplexTrace(QtCore.QThread):

    def __init__(self):
        QtCore.QThread.__init__(self)

    def get_file_input(self):
        try:
            file_name = QtGui.QFileDialog.getOpenFileName(self,"Choose a file ")
            self.lineEdit_2.setText(file_name)
            #self.lineEdit_2.setReadOnly(True)

        except:
            pass


    def write_to_hd5(self, data):
        try:
            self.progressBar.maximum(100)
            self.progressBar.setValue(0)
            self.outpath = self.lineEdit.text()
            with h5py.File(self.out_path , 'w') as self.f:
                self.f.create_dataset(self.out_name, data=data )
            self.f.close()
            self.progressBar.setValue(100)

            print(self.outpath)

        except:
            pass

    def complex_trace(self):
        try:

            self.location = str(self.lineEdit_2.text())
            self.f = h5py.File(self.location, 'r')
            self.progressBar.setValue(10)
            print(self.location)
            self.name = self.location.split('/')[-1]
            print(self.name)
            self.data = self.f.get(self.name)
            print(self.name)
            self.array = np.array(self.data)
            print(self.array.shape)

            if self.radioButton_2.isChecked():

                a = CompleTrace.ComplexAttributes()
                b = a.envelope(darray = self.array)
                c = np.array(b)
                self.write_to_hd5(c)
        except:
            pass


class ExampleApp(QtGui.QMainWindow, design.Ui_MainWindow):
    def __init__(self, parent=None):
        super(ExampleApp, self).__init__(parent)
        self.setupUi(self)
        self.extract_vol = ComplexTrace()
        self.pushButton_2.clicked.connect(lambda: self.extract_vol.get_file_input)
        self.pushButton.clicked.connect(lambda: self.extract_vol.complex_trace)

0 个答案:

没有答案