I'm writing a data acquisition program and I'm interested in keeping the GUI responsive at all times. That's why I'm using QThreads for the job. Even though the result is slightly better when comparing with case in which the whole job is done in a single thread, my GUI still hangs until the task is done. This is the relevant part of the code:
import numpy as np
from PyQt4 import QtGui, QtCore
import h5py as hdf
import tifffile as tiff
class MyWidget(QtGui.QFrame):
def __init__(self, *args, **kwargs):
super(MyWidget, self).__init__(*args, **kwargs)
self.convertButton = QtGui.QPushButton('Convert to TIFF')
self.convertButton.clicked.connect(self.convertToTiff)
recGrid = QtGui.QGridLayout()
self.setLayout(recGrid)
recGrid.addWidget(self.convertButton, 0, 0)
def convertToTiff(self):
self.converterThread = QtCore.QThread()
self.converter = TiffConverter(self.savename, self.dataname)
self.converter.moveToThread(self.converterThread)
self.converterThread.started.connect(self.converter.run)
self.converterThread.start()
class TiffConverter(QtCore.QObject):
def __init__(self, filename, dataname, *args, **kwargs):
super(TiffConverter, self).__init__(*args, **kwargs)
self.filename = filename
self.dataname = dataname
self.file = hdf.File(self.filename, mode='r')
def run(self):
tiff.imsave(self.filename, self.file[self.dataname])
self.file.close()
if __name__ == '__main__':
app = QtGui.QApplication([])
win = MyWidget()
win.show()
app.exec_()
I know the GUI hangs at tiff.imsave(self.filename, self.file[self.dataname])
and I'm familiar with the fact that QThreads aren't really doing parallel computing. Could this be the reason why the GUI hangs? Or is there a problem with sharing variables like I'm doing it? Is there any workaround for these kind of cases?