我正在尝试使用多处理池来创建对象列表,下面是简约示例:
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)
但它所做的就是抛出看起来像这样的外观:
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'>
我真的不知道该怎么做。我在这里缺少什么?