Global variable not updated in python while using multiprocessing

Viewed 1046

I have a following simple code

from multiprocessing import Pool
x = []


def func(a):
    print(x,a)


def main():
    a = [1,2,3,4,5]
    pool = Pool(1)
    global x
    x = [1,2,3,4]
    ans = pool.map(func,a)
    print(x)

It gives me the result

[] 1
[] 2
[] 3
[] 4
[] 5
[1, 2, 3, 4]

I expected the result to reflects the change in global variable x. Which seems that the changed in global variable x is not updated before the pool call. I would like to ask what is the cause of this?

3 Answers
Related