运行 简单示例时出现多处理错误

Multiprocessing error when running simple examples

我一直在尝试通过 运行 使用 multiprocessing 库的一些并行代码来优化某些功能 但由于某种原因它不起作用(在 cmd、spyder 和 jupyter 中)。

实际上我得到了一个无限循环过程和一个 AttributteError

我测试了一些简单的功能,试图了解我做错了什么,但我认为这不是代码问题,我无法弄清楚问题是什么。

谢谢!

在Pool之前定义square方法。完整示例:

➜ python3      
Python 3.6.4 (default, Mar 13 2018, 09:50:23) 
[GCC 6.3.0 20170519] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import multiprocessing as mp
>>> 
>>> def sq(x):
...     return x * x
... 
>>> with mp.Pool(2) as pool:
...     results = pool.map(sq, [1,2,3,4,5])
... 
>>> print (results)
[1, 4, 9, 16, 25]
>>>