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>Try this:</p>

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

my_var = list()


def alter_my_var(my_var, conn):
    for x in range(10):
        my_var.append(x)
        conn.send(len(my_var))
        time.sleep(1)

global my_var
parent_conn, child_conn = Pipe()
p = Process(target=alter_my_var, args=(my_var,child_conn,))
p.start()

while p.is_alive():
    myvar_len = parent_conn.recv()
    print "Length of my_var is %i" % myvar_len       
    time.sleep(1)

p.join()

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

python