我试图更好地理解类中的多处理,但我可以找到的示例使用map,而不是apply_async。我有下面的地图,但没有显示apply_async。我哪里错了?
from multiprocessing import Pool
def unwrap_self_f(arg, **kwarg):
return C.f(*arg,**kwarg)
def unwrap_self_finisher(arg, **kwarg):
return C.finisher(*arg,**kwarg)
class C:
def f(self, name):
print 'hello %s' % name
return name
def finisher(self,result):
print "done"
def runMap(self):
print "running Map..."
pool = Pool(processes=2)
names = ('frank', 'justin', 'osi', 'thomas')
pool.map(unwrap_self_f, zip([self]*len(names), names))
def runAsync(self):
print "running Async..."
pool = Pool(processes=2)
names = ('frank', 'justin', 'osi', 'thomas')
for name in names:
pool.apply_async(unwrap_self_f,args=(name),callback=unwrap_self_finisher)
pool.close()
pool.join()
if __name__ == '__main__':
c = C()
c.runMap()
c.runAsync()
答案 0 :(得分:0)
我认为你的问题是:
def unwrap_self_f(arg, **kwarg):
return C.f(*arg,**kwarg) #here you call a CLASS method of C
但您将其定义为实例方法:
class C:
def f(self, name): #self in here, so instance method
print 'hello %s' % name
return name
多处理的问题在于你没有在stderr上获得例外。