我正在使用 Python 3.6 并尝试按照下面网站上的第一个示例(下面还有完整代码)进行操作,但出现以下错误: https://docs.python.org/3.6/library/multiprocessing。网页格式
错误信息: AttributeError: module '__main__' has no attribute '__spec__'
完整示例代码:
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))
我尝试使用谷歌搜索并搜索 Stack Overflow,但我只发现了此错误的另一种情况,但没有答案。
原文由 user8474060 发布,翻译遵循 CC BY-SA 4.0 许可协议
问题不在于代码/Python 3.6,而在于 Spyder。
经过一番调查后,我发现代码在外部系统终端中执行时运行良好,但在 Spyder 的 IPython 控制台中运行时却运行不正常。
我能够转储 spec 的内容并将它们分配给包含在 main 中的变量,以允许此代码在 IPython 控制台中运行。