2 回答
TA贡献1824条经验 获得超5个赞
您需要使用Lock(请参阅本文)来保护您的值的修改。
from multiprocessing import Lock
lock = Lock()
def some_func(roll):
with lock:
if(comparison on rolls):
win.value += 1
elif(other comparison):
draw.value +=1
else:
loose.value +=1
TA贡献1946条经验 获得超4个赞
除了@jfowkes 回答的内容之外,请注意,您可以将每个 Value 与自己的锁一起使用,这可能会使事情变得更快:
win = mp.Value('i', lock = True)
draw = mp.Value('i', lock = True)
loose = mp.Value('i', lock = True)
def some_func(roll):
if(comparison on rolls):
with win.get_lock() :
win.value += 1
elif(other comparison):
with draw.get_lock():
draw.value +=1
else:
with loose.get_lock():
loose.value +=1
添加回答
举报