I'm developing a GUI which carries out some heavy number crunching. To speed up things I use joblib's Parallel execution together with pyqt's QThreads to avoid the GUI from becoming unresponsive. The Parallel execution works fine so far, but if embedded in the GUI and run in its own thread it utilizes only one of my 4 cores. Anything fundamental I missed in the threading/multiprocessing world?
Here a rough sketch of my setup:
class ThreadRunner(QtCore.QObject):
start = QtCore.pyqtSignal()
result_finished = QtCore.pyqtSignal(np.ndarray)
def __init__(self, function, *args, **kwargs):
super(DispMapRunner, self).__init__()
self.function = function
self.args = args
self.kwargs = kwargs
self.start.connect(self.run)
def run(self):
print "New Thread started"
result = self.function(*self.args, **self.kwargs)
self.result_finished.emit(result)
class Gui(QtGui.QMainWindow, form_class):
def __init__(self, cl_args, parent=None):
super(Gui, self).__init__()
#other stuff
def start_thread(self, fun, *args, **kwargs):
self.runner = ThreadRunner(fun, *args, **kwargs)
self.thread = QtCore.QThread()
self.runner.moveToThread(self.thread)
# more stuff for catching results
def slice_producer(data):
n_images, rows, cols = data.shape[:3]
for r in range(rows):
yield np.copy(data[:,r,...])
def run_parallel(data, *args, **kwargs):
results = joblib.Parallel(
n_jobs=4,
verbose=12,
pre_dispatch='1.5*n_jobs'
)
(
delayed(
memory.cache(do_long_computation))
(slice, **kwargs) for slice in slice_producer(data)
)
I hope it is not too long and at the same time too vague. I use pyqt4 4.11.3 and joblib 0.8.4.
I checked my code again and noticed the following warning:
UserWarning: Multiprocessing backed parallel loops cannot
be nested below threads, setting n_jobs=1
Which refines my question to the following: How to run a multiprocessing process in a seperate thread?