Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mjm522/1a0c44ea5901fb010354963f9ceced4c to your computer and use it in GitHub Desktop.
Save mjm522/1a0c44ea5901fb010354963f9ceced4c to your computer and use it in GitHub Desktop.
Inter process communication using multiprocessing.shared_memory
import numpy as np
from multiprocessing import shared_memory, Process
def worker(shared_variable):
existing_shm = shared_memory.SharedMemory(name=shared_variable.name)
myarray = np.ndarray((6,), dtype=np.int64, buffer=existing_shm.buf)
count = 0
while True:
count += 1
myarray[-1] = count
shm = shared_memory.SharedMemory(create=True, size=np.zeros(6).nbytes)
proc = Process(target=worker, args=(shm,))
proc.start()
while True:
read_value = np.ndarray((6,), dtype=np.int64, buffer=shm.buf)
print("Value read is ", read_value)
proc.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment