gpt4 book ai didi

python - 使用 Python 2.7/Windows 运行多处理作业

转载 作者:可可西里 更新时间:2023-11-01 11:44:35 24 4
gpt4 key购买 nike

基于 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() 和最后两行是必需的,因为没有它们,就会出现众所周知的错误:

This probably means that you are on Windows and you have forgotten to use the proper idiom in the main module:

if __name__ == '__main__':
....

但即便如此,我还是收到了这个错误:

File "C:\Users\User\Desktop\test.py", line 14, in main res = pool.map(jobWrapper, the_args) File "C:\Python27\lib\multiprocessing\pool.py", line 251, in map return self.map_async(func, iterable, chunksize).get() File "C:\Python27\lib\multiprocessing\pool.py", line 558, in get raise self._value cPickle.PicklingError: Can't pickle : attribute lookup >builtin.function failed

这里哪里涉及到cPickle,为什么会报错/如何解决?

最佳答案

所有定义都必须在模块范围内:

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()

您还需要 freeze_support 在 windows 中调用

关于python - 使用 Python 2.7/Windows 运行多处理作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48581455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com