I’m trying to create a test program that shares a given class object in Python 3 multiprocessing.
For doing this I create first a class object and then apply manager.Value to it in order to get the shared object.
The problem is that the shared object seems not to be the same object in memory outside and inside the process.
Any idea of what I’m doing wrong?
The code of the application is:
class MyClass:
def __init__(self, val):
self.val = val
def set(self, val):
print(f'set({val})')
self.val= val
def get(self):
return self.val
def modify(args):
shared, v = args
print(f'prev, get()= {shared.value.get()}')
shared.value.set(v)
print(f'after, get()= {shared.value.get()}')
def main5():
manager = Manager()
myclass = MyClass(-1)
shared = manager.Value('c', myclass)
pool = Pool(1)
params = []
for v in range(5, 6):
params.append((shared, v))
pool.imap(func=modify, iterable=params)
pool.close()
pool.join()
print("Valor modificado:", shared.value.get())
if __name__ == '__main__':
freeze_support()
main5()
And the output is:
prev, get()= -1
set(5)
after, get()= -1
Last value stored: -1
New contributor