Best way to share class attributes among processes

Viewed 939

Let's say that I have a lot of instances of a class. This class has a run function, which I am running through a Process in order to spread out work over multiple cores. The class also has an attribute c that I am accessing in the run function. The problem is, when I change one of the instance's c, it is not seen as updated in the output of the run function.

Demonstration:

import multiprocessing
class Test:
    def __init__(self):
        self.c = 0
    def run(self):
        while True:
            print self.c
test1 = Test()
p = Process(target = test1.run, args=())
p.start()
test1.c = 5

Expected output:

5
5
5
5

Actual output:

0
0
0
0

What's the best way to make sure that the variable is synced correctly among processes? (My actual code is a lot more complex - including many more objects, classes, and variables)

1 Answers
Related