Home > Back-end >  Race Condition in Python Multiprocessing Aync_apply
Race Condition in Python Multiprocessing Aync_apply

Time:04-15

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()

CodePudding user response:

No.

The callback is executed by the master process, and changes the master process makes to those variables aren't visible to the subprocesses. No subprocess touches B.

(B doesn't need to be marked global, by the way; you're not assigning to the name, but just mutating it internally.)

CodePudding user response:

Well, there aren't any race conditions because they aren't connected. Remember, these are separate processes with separate address spaces. The subprocesses will inherit the initial values, of the globals, but they are not shared.

If you need to communicate, you need to use something like a Queue to send the results back to "central control".

The multiprocessing module does have shared memory objects.

  • Related