While constructing a multiprocessing.Array the constructor takes an argument lock. It is default set to True
It is my understanding that when lock flag is set to True the multiprocessing.Array thus created should be process safe. However, i am not able to verify the behavior.
I am using the following code snippet to verify the behavior:
import multiprocessing
def withdraw(balance):
for _ in range(10000):
balance[0] = balance[0] - 1
def deposit(balance):
for _ in range(10000):
balance[0] = balance[0] + 1
balance = multiprocessing.Array('f',[1000],lock=True) ### Notice lock=True passed explicitly
p1 = multiprocessing.Process(target=withdraw, args=(balance,))
p2 = multiprocessing.Process(target=deposit, args=(balance,))
p1.start()
p2.start()
p1.join()
p2.join()
print("Final balance = {}".format(balance[0]))
Each time I run this code I am seeing different final results owing to race conditions affecting the run.
Can you please help me understand what I am doing and/or understanding wrong. As per my understanding, the code snippet I posted should always print 1000
