0

I'm trying to use the multiprocessing module in a for loop inside a PyQt class. Unfortunately, this script give me a lot of errors. Here is the script which just displays a "Run" button and starts a process in a loop which prints a value.

import multiprocessing
from PyQt4 import QtGui
import sys

class SurfViewer(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()
        self.parent = parent
        self.centralWidget = QtGui.QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX_param_scene = QtGui.QHBoxLayout()
        self.Button_Run = QtGui.QPushButton('Run')
        self.mainHBOX_param_scene.addWidget(self.Button_Run)
        self.centralWidget.setLayout(self.mainHBOX_param_scene)

        self.Button_Run.clicked.connect(self.Runclick)

    def Runclick(self):
        for i in range(5):
            p = multiprocessing.Process(target=self.mp_worker,args=(i,))
            p.start()

    def mp_worker(self,a):
        print('a:' + str(a))
        return

def main():
    app = QtGui.QApplication(sys.argv)
    ex = SurfViewer(app)
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

I don't understand what am I doing wrong here since the below code is working well:

import multiprocessing

class maclass():
    def __init__(self,):
        for i in range(5):
            p = multiprocessing.Process(target=self.mp_worker , args=(i,))
            p.start()

    def mp_worker(self,a):
        print('a:' +str(a))
        return

def main():
    maclass()

if __name__ == '__main__':
    main()

The only difference being that the multiprocess is done in Button_Run.clicked. event.

The error I get is (from the first script):

Traceback (most recent call last):

File "", line 1, in

File "C:\Anaconda2\lib\multiprocessing\forking.py", line 381, in main self = load(from_parent)

File "C:\Anaconda2\lib\pickle.py", line 1384, in load return Unpickler(file).load()

File "C:\Anaconda2\lib\pickle.py", line 864, in load dispatchkey

File "C:\Anaconda2\lib\pickle.py", line 1221, in load_build setstate = getattr(inst, "setstate", None)

RuntimeError: super-class init() of type QPushButton was never called

But I also get other error when I try to use multiprocess in my bigger application, for instance:

File "C:\Anaconda2\lib\pickle.py", line 492, in save_string

self.write(BINSTRING + pack(" i", n) + obj)

IOError: [Errno 32] Broken pipe

Maybe this one can by correlated to the first error.

Anyone already seen a solution to that issue?

Edit: I tried to use @ImportanceOfBeingErnest example below. So Here's the new code:

import multiprocessing
from PyQt4 import QtGui
import sys

class maclass():
    def __init__(self,):
        pass

    def start(self, n):
        lfp=[]
        for i in range(n):
            recv_end, send_end = multiprocessing.Pipe()
            p = multiprocessing.Process(target=self.mp_worker , args=(i,send_end))
            p.start()
            send_end.close()
            lfp.append(recv_end.recv())
            recv_end.close()
            print(i, lfp)
         #p.join()

    def mp_worker(self,a,send_end):
        print('a:' +str(a))
        send_end.send(a)
        return

class SurfViewer(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()
        self.parent = parent
        self.centralWidget = QtGui.QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX_param_scene = QtGui.QHBoxLayout()
        self.Button_Run = QtGui.QPushButton('Run')
        self.mainHBOX_param_scene.addWidget(self.Button_Run)
        self.centralWidget.setLayout(self.mainHBOX_param_scene)

        self.worker = maclass()
        self.Button_Run.clicked.connect(self.start)

    def start(self):
        for k in range(5):
            self.worker.start(4)

def main():
    app = QtGui.QApplication(sys.argv)
    ex = SurfViewer(app)
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

In this case, I would like to receive a result from the worker with recv_end, send_end = multiprocessing.Pipe(). This code works fine in the sense that I get the correct result. Unfortunately, it seems that I lose the multiprocessing ability. I'm clearly doing something wrong with the pipe, but I cannot figure out what it is.

2
  • 3
    Your worker function is a method of a Qt window. This means every new process would to have that (or a copy of) that window. Windows can not be pickeled and handed over to the new window. You have to separate your worker from the UI. Commented Nov 25, 2016 at 15:24
  • Thanks for you respond. It is the answer I did not want... ;) because I would like to use the worker as a part of the class to modify some members of that class. Commented Nov 30, 2016 at 7:18

2 Answers 2

3

You need to keep the worker separated from the UI. The following works fine. If you want to modify variables in maclass you can still do it from the outside.

import multiprocessing
from PyQt4 import QtGui
import sys

class maclass():

    def __init__(self,):
        pass

    def start(self):
        for i in range(5):
            p = multiprocessing.Process(target=self.mp_worker , args=(i,))
            p.start()

    def mp_worker(self,a):
        print('a:' +str(a))
        return


class SurfViewer(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()
        self.parent = parent
        self.centralWidget = QtGui.QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX_param_scene = QtGui.QHBoxLayout()
        self.Button_Run = QtGui.QPushButton('Run')
        self.mainHBOX_param_scene.addWidget(self.Button_Run)
        self.centralWidget.setLayout(self.mainHBOX_param_scene)

        self.worker = maclass()
        self.Button_Run.clicked.connect(self.start)

    def start(self):
        self.worker.start()


def main():
    app = QtGui.QApplication(sys.argv)
    ex = SurfViewer(app)
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
Sign up to request clarification or add additional context in comments.

1 Comment

I will try that and I come back to share if that works. Thank you.
1

I finally found a solution to my issue. Here is the code that perform the multiprocessing and also handle the result return from the multi-thread worker:

import multiprocessing
from PyQt4 import QtGui
import sys

class maclass():

    def __init__(self,):
        pass
    @classmethod
    def start(self, n):
        lfp=[]
        r=[]
        for i in range(n):
            # queue = multiprocessing.Queue()
            recv_end, send_end = multiprocessing.Pipe()
            p = multiprocessing.Process(target=self.mp_worker , args=(i,send_end))
            p.start()
            r.append(recv_end)
        for recv_end in r:
            lfp.append(recv_end.recv() )
            print(i, lfp) 
    @classmethod
    def mp_worker(self,a,send_end):
        print('a:' +str(a))
        send_end.send(a)
        return


class SurfViewer(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()
        self.parent = parent
        self.centralWidget = QtGui.QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX_param_scene = QtGui.QHBoxLayout()
        self.Button_Run = QtGui.QPushButton('Run')
        self.mainHBOX_param_scene.addWidget(self.Button_Run)
        self.centralWidget.setLayout(self.mainHBOX_param_scene)

        self.worker = maclass()
        self.Button_Run.clicked.connect(self.start)

    def start(self):
        for k in range(5):
            self.worker.start(20)



def main():
    app = QtGui.QApplication(sys.argv)
   ex = SurfViewer(app)
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

A big thanks to the people who helped me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.