基于this answer,我想用Python 2.7 / Windows运行这个def main():
import itertools as it
from multiprocessing import Pool
def dothejob(i, j, k):
print i, j, k
the_args = it.product(range(100), range(100), range(100))
pool = Pool(4)
def jobWrapper(args):
return dothejob(*args)
res = pool.map(jobWrapper, the_args)
if __name__ == '__main__':
main()
作业:
main()
if __name__ == '__main__':
....
和最后两行是必要的,因为没有它们,就会出现众所周知的错误:
这可能意味着您使用的是Windows,而且您拥有 忘记在主模块中使用正确的习语:
cPickle
但即便如此,我也会收到此错误:
文件“C:\ Users \ User \ Desktop \ test.py”,第14行,在main中 res = pool.map(jobWrapper,the_args) 文件“C:\ Python27 \ lib \ multiprocessing \ pool.py”,第251行,在地图中 return self.map_async(func,iterable,chunksize).get() 在get中输入文件“C:\ Python27 \ lib \ multiprocessing \ pool.py”,第558行 提高self._value cPickle.PicklingError:无法pickle:属性查找> 内置 .function失败
这里涉及的{{1}}在哪里以及为什么会出现此错误/如何解决?
答案 0 :(得分:2)
所有定义必须在模块范围内:
import itertools as it
from multiprocessing import Pool, freeze_support
def dothejob(i, j, k):
print i, j, k
def jobWrapper(args):
return dothejob(*args)
def main():
the_args = it.product(range(100), range(100), range(100))
pool = Pool(4)
res = pool.map(jobWrapper, the_args)
if __name__ == '__main__':
freeze_support() #you need this in windows
main()
您还需要在Windows中调用freeze_support