I'm testing some python code to share a numpy array between 2 processes.
from multiprocessing import Process, Semaphore, shared_memory
import numpy as np
import time
def reader(id, a, shm):
exst_shm = shared_memory.SharedMemory(name=shm)
b = np.ndarray(a.shape, dtype=a.dtype, buffer=exst_shm.buf)
time.sleep(2)
print('FUNCTION VERSION: ', b[0])
def worker(id, a, shm):
exst_shm = shared_memory.SharedMemory(name=shm)
b = np.ndarray(a.shape, dtype=a.dtype, buffer=exst_shm.buf)
b[0] += 10
if __name__ == "__main__":
a = np.array([0])
shm = shared_memory.SharedMemory(create=True, size=a.nbytes)
c = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf)
th1 = Process(target=reader, args=(1, a, shm.name))
th2 = Process(target=worker, args=(2, a, shm.name))
th1.start()
th2.start()
th1.join()
th2.join()
The code above works fine and it prints out: FUNCTION VERSION: 10
from multiprocessing import Process, Semaphore, shared_memory
import numpy as np
import time
class Reader(Process):
def __init__(self, id, a, shm):
Process.__init__(self)
exst_shm = shared_memory.SharedMemory(name=shm)
b = np.ndarray(a.shape, dtype=a.dtype, buffer=exst_shm.buf)
time.sleep(2)
print('SUBCLASS VERSION: ', b[0])
class Worker(Process):
def __init__(self, id, a, shm):
Process.__init__(self)
exst_shm = shared_memory.SharedMemory(name=shm)
b = np.ndarray(a.shape, dtype=a.dtype, buffer=exst_shm.buf)
b[0] += 10
if __name__ == "__main__":
a = np.array([0])
shm = shared_memory.SharedMemory(create=True, size=a.nbytes)
c = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf)
th1 = Reader(1, a, shm.name)
th2 = Worker(2, a, shm.name)
th1.start()
th2.start()
th1.join()
th2.join()
However, when it's written as classes, it prints out: SUBCLASS VERSION: 0. Where does the difference come from and what is wrong with the code?
question from:
https://stackoverflow.com/questions/65942502/python-multiprocessing-and-shared-memory-why-do-i-get-different-results