In Python 3.8+, is it possible to check whether a numpy array is being stored in shared memory?
In the following example, a numpy array sharedArr
was created using the buffer of a multiprocessing.shared_memory.SharedMemory
object. Will like to know if we can write a function that can detect whether SharedMemory
is used.
import numpy as np
from multiprocessing import shared_memory
if __name__ == '__main__':
# Created numpy array `sharedArr`in shared memory
arr = np.zeros(5)
shm = shared_memory.SharedMemory(create=True, size=arr.nbytes)
sharedArr = np.ndarray(arr.shape, dtype=arr.dtype, buffer=shm.buf)
sharedArr[:] = arr[:]
# How to tell if numpy array is stored in shared memory?
print(type(sharedArr)) # <class 'numpy.ndarray'>
print(hex(id(sharedArr))) # 0x7fac99469f30
shm.close()
shm.unlink()