gpt4 book ai didi

python - 多重处理导致 PicklingError

转载 作者:行者123 更新时间:2023-11-28 23:00:21 25 4
gpt4 key购买 nike

执行示例

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

最佳答案

这是一个 known bug在 IPython 中。您正在从 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 运行。

关于python - 多重处理导致 PicklingError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12161437/

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