Python multithreading doesn't use common memory when the parent object is created inside a process
<p>I'm trying to implement a thread which will run and do a task in the background. Here's my code which gives expected output.</p>

<p><strong>Code 1:</strong></p>

<pre><code>from time import sleep
from threading import Thread

class myTest( ):
    def myThread( self ):
        while True:
            sleep( 1 )
            print self.myList.keys( )
            if 'exit' in self.myList.keys( ):
                break
        return

    def __init__( self ):
        self.myList = { } 
        self.thread = Thread( target = self.myThread, args = (  ) )
        self.thread.start( )
        return

    def myFun( self ):
        i = 0
        while True:
            sleep( 0.5 )
            self.myList[ i ] = i
            i += 1
            if i &gt; 5 :
                self.myList[ 'exit' ] = ''
                break
        return

x = myTest( )
x.myFun( )
</code></pre>

<p><strong>Output 1:</strong></p>

<pre><code>[0]
[0, 1, 2]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5, 'exit']
</code></pre>

<p>The moment I create a multi-process environment and create this object in the new sub-process, the thread is unable to access the common memory and the dictionary <code>myList</code> remains blank in the thread. Here is the code.</p>

<p><strong>Code 2:</strong></p>

<pre><code>from time import sleep
from threading import Thread
from multiprocessing import Process

class myTest( ):
    def myThread( self ):
        while True:
            sleep( 1 )
            print "myThread", self.myList.keys( )
            if 'exit' in self.myList.keys( ):
                break
        return

    def __init__( self ):
        self.myList = { } 
        self.thread = Thread( target = self.myThread, args = (  ) )
        self.thread.start( )
        self.i = 0
        return

    def myFun( self ):
        self.myList[ self.i ] = self.i
        self.i += 1
        if self.i &gt; 5 :
            self.myList[ 'exit' ] = ''
            return 'exit'
        print 'myFun', self.myList.keys( )
        return

class myProcess( Process ):
    def __init__( self ):
        super( myProcess, self ).__init__( )
        self.myObject = myTest( )
        return

    def run(self):
        while True:
            sleep( 0.5 )
            x = self.myObject.myFun( )
            if x == 'exit':
                break
        return

x = myProcess( )
x.start( )
</code></pre>

<p><strong>Output 2:</strong></p>

<pre><code>myFun [0]
myThread []
myFun [0, 1]
myFun [0, 1, 2]
myThread []
myFun [0, 1, 2, 3]
myFun [0, 1, 2, 3, 4]
myThread []
myThread []
myThread []
... ... ...
... ... ...
</code></pre>

<p>In the 1st code, the object is created inside a Python process (Though main process). In the 2nd code, the object is created inside a sub-process of Python where everything takes place which should have behaved just like the first one.</p>

<ol>
<li>Can anybody explain why in the 2nd code, the thread is unable to use the shared memory of the parent object?</li>
<li>I want the <code>output 1</code> in <code>code 2</code> i.e. force the thread to use the shared common memory of the parent object. What modification do I need to make?</li>
</ol>

<p>When you spawn the second process, is created about a <strong>copy</strong> of the first one, so <strong><em>the threads spawned in the 2nd process can only access that process stack</em></strong>.</p>

<p>In <strong>code2</strong> The object of <code>myTest</code> is created in the constructor (<code>__init__</code>), so it won't share the same memory as of the spawned process which is <code>run</code>. Just by changing the position of the object creation from constructor to <code>run</code>, the object created becomes part of the spawned process memory.</p>

python multithreading multiprocessing