Because TestProcess.run()
is running in a child process, it assigns self.bar
in the child process, which cannot effect parent process.
The following code will show you the different:
import os
from multiprocessing import Process, Queue
class TestProcess( Process ):
def __init__( self, foo ):
super( TestProcess, self ).__init__()
self.foo = foo
self.bar = None
self.que = Queue()
def run( self ):
from time import sleep
sleep( 1 )
self.bar = self.foo
print( "pid2=%s, p.bar=%s" % (os.getpid(), self.bar ))
self.que.put(self.bar)
p = TestProcess( 5 )
p.start()
p.join()
print( "pid1=%s, p.bar=%s" % (os.getpid(), p.bar ))
print(p.que.get())
You will see pid1 and pid2 are different. And, of course, if you use Thread, they run in same process, so it works.
If you do need store data in child process, then transfer to parent process, then involve inter-process-communication. You can use Queue.