【来源】https://stackoverflow.com/questions/45720153/python-multiprocessing-error-attributeerror-module-main-has-no-attribute
错误信息: 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]))
解决方法
增加一行 __spec__ = "ModuleSpec(name='builtins', loader=)" 即可
修改后代码
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
__spec__ = "ModuleSpec(name='builtins', loader=)"
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))