我正在python中运行一个非常基本的multiprocessing
命令。
我只是尝试在shell中运行以下脚本。一个错误,一个没有。
from multiprocessing import Pool
def main():
mylist = ['Hello', 'You', 'Are', 'Human']
def cube(x):
return {'This': x}
pool = Pool(processes=4)
results = [pool.apply_async(cube, args=(x,)) for x in mylist]
output = [p.get() for p in results]
return output
main()
这将返回以下错误(也尝试使用if __name__ == '__main__'
,同样的错误):
Exception in thread Thread-2:
Traceback (most recent call last):
File "/apps/Linux64/python2.7/lib/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
File "/apps/Linux64/python2.7/lib/python2.7/threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
File "/apps/Linux64/python2.7/lib/python2.7/multiprocessing/pool.py", line 319, in _handle_tasks
put(task)
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
然后我运行了这个
mylist = ['Hello', 'You', 'Are', 'Human']
def cube(x):
return {'This': x}
pool = Pool(processes=4)
results = [pool.apply_async(cube, args=(x,)) for x in mylist]
output = [p.get() for p in results]
print output
我得到了:
[{'This': 'Hello'}, {'This': 'You'}, {'This': 'Are'}, {'This': 'Human'}]