Could there be a race condition in the following code such that two async processes both try to update the global variable B at the same time in the call back function? If so, does python handle this or is it something we have to handle using locks? I have run this piece of code several times and have not had a race condition occur (that I know about) but not sure if that means it is impossible to happen.
Code:
import multiprocessing as mp
ky = 0
B = {}
def update(u):
global B
global ky
B[ky] = u
ky += 1
def square(x, y):
return x*y
def runMlt():
pool = mp.Pool(2)
for i in range(10):
pool.apply_async(square, args=(i, i + 1), callback=update)
pool.close()
pool.join()
if __name__ == '__main__':
runMlt()