我正在使用Python 3.6,并且正在尝试跟随下面网站上的第一个示例(下面的完整代码)并且我得到以下错误:https: //docs.python.org/3.6/library/multiprocessing. HTML
错误信息:
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]))
Run Code Online (Sandbox Code Playgroud)
我尝试使用Google搜索并搜索Stack Overflow,但我只发现了另一个此错误的情况并且没有答案.
我已经通过将导入移到顶部声明来解决我的问题,但它让我想知道:为什么我不能使用'__main__'在作为目标的函数中导入的模块multiprocessing?
例如:
import os
import multiprocessing as mp
def run(in_file, out_dir, out_q):
arcpy.RaterToPolygon_conversion(in_file, out_dir, "NO_SIMPIFY", "Value")
status = str("Done with "+os.path.basename(in_file))
out_q.put(status, block=False)
if __name__ == '__main__':
raw_input("Program may hang, press Enter to import ArcPy...")
import arcpy
q = mp.Queue()
_file = path/to/file
_dir = path/to/dir
# There are actually lots of files in a loop to build
# processes but I just do one for context here
p = mp.Process(target=run, args=(_file, _dir, q))
p.start()
# …Run Code Online (Sandbox Code Playgroud)