每当给函数A.f提供输入时,我都试图更新count的值。我试图以这种方式使用锁定方法,但结果不正确。请帮助我。
将多处理导入为mp
class Trial():
lock = mp.Lock()
def __init__(self):
self.a = 1
self.b = 3
with self.lock:
self.count = 0
def f(self,x):
self.a += x
self.action(x)
with self.lock:
self.count += 1
return self.a,self.b,self.count
def action(self,act):
self.b *= act
if __name__ == '__main__':
this = list([1,2,3,4,5])
A = Trial()
pool = mp.Pool()
result = pool.map(func=A.f, iterable=this)
print(result)
[(2,3,1),(3,6,1),(4,9,1),(5,12,1),(6,15,1)]
我对每个过程都获得相同的计数值。我认为每个流程都在使用自己的count副本。如何使计数对所有过程通用?