基于this answer,我想用Python 2.7 / Windows运行这个 multiprocessing
作业:
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()
和最后两行是必要的,因为没有它们,就会出现众所周知的错误:
这可能意味着您在Windows上并且忘记在主模块中使用正确的习惯用法:if name =='__ main__':
....
但即使这样,我也会收到此错误:
文件“C:\ Users \ User \ Desktop \ test.py”,第14行,在主res = pool.map(jobWrapper,the_args)文件“C:\ Python27 \ lib \ multiprocessing \ pool.py”,第251行,在map中返回self.map_async(func,iterable,chunksize).get()文件“C:\ Python27 \ lib \ multiprocessing \ pool.py”,第558行,在get raise self._value cPickle.PicklingError:不能pickle:属性查找> builtin.function失败
Where is there a cPickle involved here and why this error / how to solve it?
1 回答
所有定义必须在模块范围内:
你还需要在windows中调用
freeze_support