Access a global variable to track status in multiprocessing
<p>I am writing a multiprocessing process that I want to monitor the status of.  How can I access my_var from thaht context?</p>

<pre><code>from multiprocessing import Process
import time

my_var = list()


def alter_my_var():
    global my_var
    for x in range(10):
        my_var.append(x)
        time.sleep(1)

p = Process(target=alter_my_var)
p.start()

while p.is_alive():
    print "Length of my_var is %i" % len(my_var)
    time.sleep(1)

p.join()

print "Done - final length of my_var is %s" % len(my_var)
</code></pre>

<p>Thanks</p>

<p>You are using <code>multiprocessing</code> not <code>threading</code>. Process run in a different memory space and variables get copied to the child process but they do not point to the same memory address anymore. Therefore, what you modify in the process can not be read by the main process. </p>

<p>If you don't care if it's a process or a thread try changing <code>Process</code> by <code>Thread</code> and it will work.</p>

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

<pre><code>Length of my_var is 0
Length of my_var is 1
Length of my_var is 2
Length of my_var is 3
Length of my_var is 4
Length of my_var is 6
Length of my_var is 7
Length of my_var is 8
Length of my_var is 9
Length of my_var is 10
Done - final length of my_var is 10
</code></pre>

<p><strong>Edit</strong></p>

<p>As other people say, if you want to keep using a process you have to build some kind of IPC. You can use for that queues, the Manager, etc.</p>

python