我假设您正在谈论收到此错误:
hello function
Process Process-1:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/local/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "mp2.py", line 8, in f
dict['process_obj'] = multiprocessing.current_process()
File "<string>", line 2, in __setitem__
File "/usr/local/lib/python2.7/multiprocessing/managers.py", line 758, in _callmethod
conn.send((self._id, methodname, args, kwds))
PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed
(在问题中包含“我得到了什么”和“我期望得到什么”通常是一个好主意)。
这里的根本问题是multiprocessing.current_process()
返回一个实例方法。实例方法不能正确腌制,并且multiprocessing
必须保存(腌制)和加载(取消腌制)共享数据项才能将它们的值从一个进程传递到另一个进程。例如,请参阅Can't pickle <type 'instancemethod'> when using python's multiprocessing Pool.map()和Overcoming Python's limits about instance methods。特别注意第二个答案之一:找出一些要发送/共享的状态可能会更好,而不是整个实例。例如,如果ident
一个进程的 足够了,你可以这样做:
dict['process_obj'] = multiprocessing.current_process().ident
效果很好。