0

Starting from where a following thread concluded Can't pickle type instancemethod that discussed a similar issue.

Here is an example code (taken from that post(simplified) that does solve a problem of using multipocessing from inside of class:

from multiprocessing import Pool, cpu_count 
from multiprocessing.pool import ApplyResult

class Myclass(object):    
    def __init__(self):    
        pool = Pool(processes=3)    
        async_results = [ pool.apply_async(self, (i,)) for i in range(8) ]    
        pool.close()    
        map(ApplyResult.wait, async_results)   

    def __call__(self, i):
        self.process_obj(i)

    def __del__(self):
        print "... Destructor"

    def process_obj(self, i):
        print "obj %d" % i
        return "result"

Myclass()

Next trying a same code except this time Myclass() is inheriting from QtGui.QMainWindow:

from PyQt4 import QtCore, QtGui

from multiprocessing import Pool, cpu_count
from multiprocessing.pool import ApplyResult


app = QtGui.QApplication(sys.argv)

class Myclass(QtGui.QMainWindow):
    def __init__(self):
        super(Myclass, self).__init__()
        pool = Pool(processes=3)
        async_results = [ pool.apply_async(self, (i,)) for i in range(8) ]
        pool.close()
        map(ApplyResult.wait, async_results)

    def __call__(self, i):
        self.process_obj(i)

    def __del__(self):
        print "... Destructor"

    def process_obj(self, i):
        print "obj %d" % i
        return "result"

Myclass()

Running it results to RuntimeError:

 RuntimeError: super-class __init__() of type Myclass was never called

Would would be a possible solution?

Community
  • 1
  • 1
alphanumeric
  • 17,967
  • 64
  • 244
  • 392
  • 2
    possible duplicate of [Can't pickle when using python's multiprocessing Pool.map()](http://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod-when-using-pythons-multiprocessing-pool-ma) – g.d.d.c Mar 07 '14 at 00:29
  • It could be considered a duplicate. But I see there are a lot of working and non-working ideas bounced around in the post you refer to before a solution was made. Here I am posting a concrete example with the simplest syntax possible. This posted example could serve as a short quick clear answer to anyone who would be looking for it in a future. – alphanumeric Mar 07 '14 at 00:55
  • Thanks g.d.d.c. I've just revisited my question. I hope that would make it more valuable. – alphanumeric Mar 07 '14 at 01:41
  • Is this related to `multiprocessing` at all? Have you tried removing everything in `__init__` but the `super` call? – Midnighter Mar 07 '14 at 01:50
  • How beneficial it would to remove everything in __init__ but the super call from Myclass()? – alphanumeric Mar 07 '14 at 01:58

0 Answers0