2

When using multiprocessing in Python, I hope to make a list of a class as a shared variable. The class can be seen as follows(simplified):

class testClass():
def __init__(self):
    self.a = []
    self.b = []

Then, I use multiprocessing to process the list of 'testClass':

from multiprocessing import Process, Manager

def testProc(a_list):
    for i in range(10):
        a_list[1].a.append([1])
        print('a_list[1] address: in processing', a_list[1])

if __name__ == '__main__':
    manager = Manager()
    testList = [testClass() for i in range(4)]
    a_list = manager.list(testList)
    print('a_list[1] address: initial ', a_list[1])
    process_0 = Process(target=testProc, args=(a_list,))
    process_0.start()
    process_0.join()
    print('a_list[1] address: final ', a_list[1])

Then the result is :

a_list[1] address: initial <__main__.testClass object at 0x7f666c160f60>
a_list[1] address: in processing <__main__.testClass object at 0x7f666c16cf28>
a_list[1] address: in processing <__main__.testClass object at 0x7f666c16cf28>
a_list[1] address: final <__main__.testClass object at 0x7f666c160f98>

It can be seen that, the address of the element of the list can vary from place to place, which fail to act as shared variable. How can I solve this problem?

Tianqing
  • 21
  • 1
  • @Tianqing - They cannot have the same address in different processes. What `multiprocessing.Manager` does is wrapping the target object so it can synchronize the values between processes using pipes or networking, but the 'wrapped' objects will have different addresses in different processes. – zwer Jun 29 '18 at 12:28
  • On a side note: you know the first list element has index 0, not 1, right? – jsbueno Jun 29 '18 at 12:36
  • @jsbueno Sure, I'm just using this as an example. Thanks anyway! – Tianqing Jun 29 '18 at 14:18
  • @zwer Thanks, I now know the reasons for the different address. But what if I wan t to modify the values of the objects in the list when doing multi processing? Is there any ways? – Tianqing Jun 29 '18 at 14:30

1 Answers1

0

Move the a and b class attributes outside of __init__ function

class testClass(): a = [] b = []

In this way, a and b will be class variables, shared by all the testClass instances.

Class and Instance Variables in Python

Dan Lupascu
  • 308
  • 1
  • 3
  • 9