多处理导致PicklingError

时间:2012-08-28 14:23:37

标签: python multiprocessing pickle

执行示例

from multiprocessing import Process

def f(name):
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()
来自包multiprocessing的python文档的

会产生PicklingError: Can't pickle <function f at 0x05A57830>: it's not found as __main__.f

我该如何解决这个问题?

完整的错误消息是

Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\startup.py", line 128, in runfile
    execfile(filename, glbs)
  File "C:\Users\Ruben\Desktop\untitled0.py", line 15, in <module>
    p.start()
  File "C:\Python27\lib\site-packages\multiprocessing-2.6.2.1-py2.7-win32.egg\multiprocessing\process.py", line 109, in start
    self._popen = Popen(self)
  File "C:\Python27\lib\site-packages\multiprocessing-2.6.2.1-py2.7-win32.egg\multiprocessing\forking.py", line 244, in __init__
    dump(process_obj, to_child, HIGHEST_PROTOCOL)
  File "C:\Python27\lib\site-packages\multiprocessing-2.6.2.1-py2.7-win32.egg\multiprocessing\forking.py", line 167, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "C:\Python27\lib\pickle.py", line 224, in dump
    self.save(obj)
  File "C:\Python27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "C:\Python27\lib\pickle.py", line 419, in save_reduce
    save(state)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 681, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 748, in save_global
    (obj, module, name))
PicklingError: Can't pickle <function f at 0x05A57830>: it's not found as __main__.f

1 个答案:

答案 0 :(得分:4)

这是IPython中的known bug。您正在运行ipython控制台中的代码:

File "<ipython console>", line 1, in <module>

其中__main__ references a FakeModule和pickle找不到__main__.f

PicklingError: Can't pickle <function f at 0x05A57830>: it's not found as __main__.f

multiprocessing使用pickle将函数和参数传递给工作进程。)

因此,解决方案是将代码作为脚本运行,而不是从ipython运行。