I was testing python multiprocessing with a simple program:
class MyTest:
def __init__(self):
self.myattr = 1
def myfunc(self):
print(self)
self.myattr=2
print(self)
print("in proc"+str(self.myattr))
def main():
test = MyTest()
print(test)
myProc = Process(target = test.myfunc,args=())
myProc.start()
myProc.join()
print(test.myattr)
main()
The output of self in the method that the process is running has the same address as that of the object in main, which I found quite strange since they are separate processes. However, since they are at the same address, they are in fact shared across processes and updating the attribute myattr in the myfunc should change the attr of test in main. However print(test.myattr)
still prints 1 even after the process has finished updating test
's attribute.
My first question is why do these two objects in separate processes share the same address, and my second is if they do share the same address why aren't changes in one process recognized by changes in another?
P.S. does my first question have something to do with the fact that I am passing the process a object method to run as its target? i.e. a object function call such as test.myfunc = MyTest.myfunc(test).
The output:
<__main__.MyTest object at 0x7f007bdf76d8>
<__main__.MyTest object at 0x7f007bdf76d8>
<__main__.MyTest object at 0x7f007bdf76d8>
in proc2
1