0

我试图更好地理解类中的多处理,但我可以找到的示例使用 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()
4

1 回答 1

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 上获得异常。

于 2013-09-04T18:12:02.773 回答