The best way to share class attributes among processes
Let's say I have many instances of a class. This class has a function run
that I run in a Process to distribute work across multiple cores. The class also has an attribute c
that I refer to in the function run
. The problem is that when I change one of the instances c
, it is not seen as updated in the output of the function run
.
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 Result:
5 5 5 5
Actual output:
0 0 0 0
What's the best way to make sure a variable is properly synchronized across processes? (My actual code is much more complex - including many objects, classes and variables)
source to share
multiprocessing
provides an API for easily regenerating subprocesses, not threads. It's not just semantics; the reason you are not getting the results you expect is because the individual processes do not share memory spaces (where the threads run).
Here is a quick example to demonstrate. The thread is the one that prints 5, while the subprocess prints 0.
Here is the documentation for the threading library, which I believe is what you are looking for.
source to share