I was running this code piece in python terminal, and it gave me the following error:
AttributeError: Can't get attribute 'cube' on <module '__main__' (built-in)>
The code and the error message are as the following:
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def cube(x):
... return x**3
...
>>> import multiprocessing as mp
>>> pool = mp.Pool(processes=4)
>>> res = [pool.apply(cube, args=(x,)) for x in range(1,7)]
Process SpawnPoolWorker-1:
Traceback (most recent call last):
File "/opt/anaconda3/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/opt/anaconda3/lib/python3.8/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/opt/anaconda3/lib/python3.8/multiprocessing/pool.py", line 114, in worker
task = get()
File "/opt/anaconda3/lib/python3.8/multiprocessing/queues.py", line 358, in get
return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'cube' on <module '__main__' (built-in)>
Now, after searching this link: multiprocessing error "AttributeError: Can't get attribute 'testfuncxx' on <module '__main__",
I update the above code as the following and run it through the text editor Atom,
import multiprocessing, time
def cube(num):
time.sleep(num)
print(num**3)
def apply_async_callback():
pool = multiprocessing.Pool(processes=4)
for i in range(10):
pool.apply_async(cube, args=(i,))
pool.close() # close the use of pool, to stop inserting processes into this pool
pool.join() # continue processing each subprocesses, until this subprocess has been finished
if __name__=='__main__':
apply_async_callback()
It works. I was wondering, could anyone please explain to me why is it? Moreover, did I understand the difference between pool.close() and pool.join() function correctly?
question from:
https://stackoverflow.com/questions/65660686/attributeerror-cant-get-attribute-cube-on-module-main-built-in-whe 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…