Skip to content

Instantly share code, notes, and snippets.

@bml1g12
Last active March 9, 2021 18:59
Show Gist options
  • Save bml1g12/54b448772a077d15be5ab9a710f28cf0 to your computer and use it in GitHub Desktop.
Save bml1g12/54b448772a077d15be5ab9a710f28cf0 to your computer and use it in GitHub Desktop.
Shared memory for communicating Numpy arrays
def worker_producer_shared_memory(np_arr_shape, shared_memory, n_frames):
"""A frame producer function that writes to shared memory"""
mp_array, np_array = shared_memory
for _ in range(n_frames):
mp_array.acquire()
np_array[:] = prepare_random_frame(np_arr_shape) # produce a fresh array
def consumer_shared_memory(n_frames, shared_memory):
"""A frame consumer function, which draws frames from the worker process via shared memory."""
mp_array, np_array = shared_memory
for _ in range(n_frames):
_ = np_array.astype("uint8").copy() * 2 # example of some processing done on the array
while True:
try:
mp_array.release()
break
# it already unlocked, wait until its locked again which means a new frame is ready
except ValueError:
time.sleep(0.001)
mp_array = mp.Array("I", int(np.prod(np_arr_shape)), lock=mp.Lock())
# create a numpy view of the array
np_array = np.frombuffer(mp_array.get_obj(), dtype="I").reshape(np_arr_shape)
shared_memory = (mp_array, np_array)
proc = mp.Process(target=worker_producer_shared_memory,
args=(np_arr_shape, shared_memory, n_frames))
proc.start()
# will consume n_frames from the producer
consumer_shared_memory(n_frames, shared_memory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment