1

I am trying to use the multiprocessing pool to create a list of objects, minimalistic example below:

import multiprocessing as mp

class MyClass:
    Count = 0

    def __init__(self, num):
        self.num = num
        MyClass.Count += 1

def CreateObj(num):
    Obj = MyClass(num)
    return Obj

if __name__ == '__main__':
    #x = CreateObj(5)

    pool = mp.Pool()
    x = pool.map(CreateObj, [1, 2, 3])

    print(x)

but all it does is throwing exeptions that look like this:

Exception in thread Thread-8:
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\pool.py", line 429, in _handle_results
    task = get()
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\connection.py", line 251, in recv
    return _ForkingPickler.loads(buf.getbuffer())
AttributeError: Can't get attribute 'MyClass' on <module '__main__' from 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\spyder\\utils\\ipython\\start_kernel.py'>

I don't know what to make of this really. What am I missing here?

VY_CMa
  • 145
  • 1
  • 9
  • Your program seems to [work for me](https://ideone.com/SshwFY). What environment are you running in? OS? IDE? Any other environmental factors? – Robᵩ Jan 03 '18 at 19:39
  • Do you expect `MyClass.Count` to be shared between processes? You need to use special primitives. See [**`multiprocessing`**: Sharing state between processes](https://docs.python.org/2/library/multiprocessing.html#sharing-state-between-processes) – Peter Wood Jan 03 '18 at 19:42
  • I'm running on Win10 in Spyder 3.1.2. – VY_CMa Jan 03 '18 at 19:45
  • And yes, I do expect it to be shared, but have not gotten far enough to see if that is working, thanks for pointing it out – VY_CMa Jan 03 '18 at 19:46
  • 1
    Spyder is based on IPython, right? If so, I think this explains the issue: https://github.com/ipython/ipython/issues/10894 – Robᵩ Jan 03 '18 at 19:47
  • I executed it in a seperate Python (non-IPython) console and it appears to work for now, thanks for the quick answer! – VY_CMa Jan 03 '18 at 19:56
  • @Robᵩ can you put that into an answer? I see a lot of ipython and multiprocessing questions since a few days – hansaplast Jan 03 '18 at 20:49

0 Answers0