以下不能使用python 2.7.9,但也不會拋出任何錯誤或異常。有沒有一個錯誤,或者不能在一個類中使用多處理?Python多處理沉默失敗類
from multiprocessing import Pool
def testNonClass(arg):
print "running %s" % arg
return arg
def nonClassCallback(result):
print "Got result %s" % result
class Foo:
def __init__(self):
po = Pool()
for i in xrange(1, 3):
po.apply_async(self.det, (i,), callback=self.cb)
po.close()
po.join()
print "done with class"
po = Pool()
for i in xrange(1, 3):
po.apply_async(testNonClass, (i,), callback=nonClassCallback)
po.close()
po.join()
def cb(self, r):
print "callback with %s" % r
def det(self, M):
print "method"
return M+2
if __name__ == "__main__":
Foo()
運行打印此:
done with class
running 1
running 2
Got result 1
Got result 2
編輯:這似乎是相關的,但它使用.map
,而我特別感到需要使用apply_async
這似乎在多與類的實例是如何工作方面關係(例如,我沒有picklnig錯誤,就像很多其他相關問題一樣) - Python how to do multiprocessing inside of a class?
你爲什麼要創建兩個不同的池?在類塊中創建池可能不是一個好主意。你可以在__init__或__name__ ==「__main __」塊中完成。當你說「不行」時,你的意思是什麼? –