問題:
I'm trying to use multiprocessing
's Pool.map()
function to divide out work simultaneously.我正在嘗試使用multiprocessing
的Pool.map()
函數來同時劃分工作。 When I use the following code, it works fine:當我使用以下代碼時,它工作正常:
import multiprocessing
def f(x):
return x*x
def go():
pool = multiprocessing.Pool(processes=4)
print pool.map(f, range(10))
if __name__== '__main__' :
go()
However, when I use it in a more object-oriented approach, it doesn't work.但是,當我以更面向對象的方法使用它時,它不起作用。 The error message it gives is:它給出的錯誤信息是:
PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup
__builtin__.instancemethod failed
This occurs when the following is my main program:當以下是我的主程序時會發生這種情況:
import someClass
if __name__== '__main__' :
sc = someClass.someClass()
sc.go()
and the following is my someClass
class:以下是我的someClass
類:
import multiprocessing
class someClass(object):
def __init__(self):
pass
def f(self, x):
return x*x
def go(self):
pool = multiprocessing.Pool(processes=4)
print pool.map(self.f, range(10))
Anyone know what the problem could be, or an easy way around it?任何人都知道問題可能是什麼,或者解決它的簡單方法?
解決方案:
參考一: https://en.stackoom.com/question/7cfm參考二: https://stackoom.com/question/7cfm