我有這個使用多處理映射的簡單例子。但即使如此,我也無法正確運行。多處理映射引發異常
import multiprocessing
p = multiprocessing.Pool()
rere = range(50)
print p.map(lambda x: x+1, rere)
它將打印此異常:
Exception in thread Thread-3:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 808, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 761, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/lib/python2.7/multiprocessing/pool.py", line 342, in _handle_tasks
put(task)
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
並且不能使用Ctrl + C終止。
我應該如何解決我的例子,使其工作?
是啊,我跳槍對不起。它與multiprocessing.pool.map的工作方式有關,「lambda x:x + 1」部分必須是pickleable,而不是函數,我們都需要更多的實驗! – TehTris
http://stackoverflow.com/questions/8804830/python-multiprocessing-pickling-error?rq=1檢查一下,它幾乎完全相同 – TehTris
聲明函數爲模塊中的頂層我得到這個: AttributeError: 'module'對象沒有屬性'f'(當然我的函數被稱爲f,並聲稱它不存在是由於某種原因)。 – LtWorf