python class variable not updated when passed to new Process

39 Views Asked by At

Why doesn't a new Process get the updated class attr value (in Windows btw). (It's the same if the attr was just a str). I'm guessing the 3rd case worked because assignment gave it a instance attr.

https://stackoverflow.com/a/7525883/1582790 This post says something about the static/class members but didn't explain why. (The bit about setting the class member using instance.attr= seems incorrect as seen in my third case)

 

class A:
    a1 = {}

def fp(a):
    print(a.a1)

if __name__ == '__main__':
    a = A()
    A.a1.update({"p":"1"}) # update class var
    p1 = Process(target=fp, args=(a,))
    p1.start()

    A.a1 = {"p":"2"}
    # print('--', a.a1) # {'p': '2'}
    p2 = Process(target=fp, args=(a,))
    p2.start()

    a.a1 = {"p":"3"} # doesn't change A.a1
    # print('--', A.a1) # {'p': '2'}
    p3 = Process(target=fp, args=(a,))
    p3.start()

prints:

{}
{}
{'p': '3'}
0

There are 0 best solutions below