dmcmulle dmcmulle - 1 year ago 110
Python Question

Access a global variable to track status in multiprocessing

I am writing a multiprocessing process that I want to monitor the status of. How can I access my_var from thaht context?

from multiprocessing import Process
import time

my_var = list()

def alter_my_var():
global my_var
for x in range(10):

p = Process(target=alter_my_var)

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


print "Done - final length of my_var is %s" % len(my_var)


Answer Source

You are using multiprocessing not threading. 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.

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


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


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download