在函数内部运行代码时出现PicklingError

2024-06-05 18:52:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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'}]


Tags: inselfhelloforoutputmaindef错误