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?