multiprocessing: child process share changes to global variable

Alan alan.isaac at
Mon Dec 2 23:34:05 CET 2013

In the code below, child processes see changes to global variables caused by other child processes. That is, is producing a list where the value of ``lst`` is being (non-deterministically) shared across child processes.  Standard advice is that, "Processes have independent memory space" and "each process works with its own copy" of global variables, so I thought this was not supposed to happen. In the end, the globals are not changed, which matches my expectation. Still, clearly I have misunderstood what seemed to be standard advice.  Can someone clarify for me?

Expected result from [[0],[1],[2]]
Usual result: [[0],[0,1],[0,1,2]]
Occasional result: [[0],[1],[0,2]]

Alan Isaac

#--------------- -------------------------
#run at Python 2.7 command prompt
import time
import multiprocessing as mp
lst = []
lstlst = []

def alist(x):
    print "a"
    return lst

if __name__=='__main__':
    pool = mp.Pool(3)
    print,range(3)) #UNEXPECTED RESULTS
    print "b"

print "c"
print lst
print lstlst

More information about the Python-list mailing list