def ndarray_shm(shape, dtype, location, readonly=False, order='F', **kwargs):
  """Create a shared memory numpy array. Requires /dev/shm to exist."""
  nbytes = Vec(*shape).rectVolume() * np.dtype(dtype).itemsize
  available = psutil.virtual_memory().available

  preexisting = 0
  # This might only work on Ubuntu
  shmloc = os.path.join(SHM_DIRECTORY, location)
  if os.path.exists(shmloc):
    preexisting = os.path.getsize(shmloc)
  elif readonly:
    raise SharedMemoryReadError(shmloc + " has not been allocated. Requested " + str(nbytes) + " bytes.")

  if readonly and preexisting != nbytes:
    raise SharedMemoryReadError("{} exists, but the allocation size ({} bytes) does not match the request ({} bytes).".format(
      shmloc, preexisting, nbytes
    ))

  if (nbytes - preexisting) > available:
    overallocated = nbytes - preexisting - available
    overpercent = (100 * overallocated / (preexisting + available))
    raise SharedMemoryAllocationError("""
      Requested more memory than is available. 

      Shared Memory Location:  {}

      Shape:                   {}
      Requested Bytes:         {} 
      
      Available Bytes:         {} 
      Preexisting Bytes*:      {} 

      Overallocated Bytes*:    {} (+{:.2f}%)

      * Preexisting is only correct on linux systems that support /dev/shm/""" \
        .format(location, shape, nbytes, available, preexisting, overallocated, overpercent))

  # This might seem like we're being "extra safe" but consider
  # a threading condition where the condition of the shared memory
  # was adjusted between the check above and now. Better to make sure
  # that we don't accidently change anything if readonly is set.
  flags = 0 if readonly else O_CREAT 
  size = 0 if readonly else int(nbytes) 

  try:
    shared = posix_ipc.SharedMemory(location, flags=flags, size=size)
    array_like = mmap.mmap(shared.fd, shared.size)
    os.close(shared.fd)
    renderbuffer = np.ndarray(buffer=array_like, dtype=dtype, shape=shape, order=order, **kwargs)
  except OSError as err:
    if err.errno == errno.ENOMEM: # Out of Memory
      posix_ipc.unlink_shared_memory(location)      
    raise

  renderbuffer.setflags(write=(not readonly))
  return array_like, renderbuffer
def ndarray_shm ( shape , dtype , location , readonly = False , order = 'F' , ** kwargs ) : nbytes = Vec ( * shape ) . rectVolume ( ) * np . dtype ( dtype ) . itemsize available = psutil . virtual_memory ( ) . available preexisting = 0 shmloc = os . path . join ( SHM_DIRECTORY , location ) if os . path . exists ( shmloc ) : preexisting = os . path . getsize ( shmloc ) elif readonly : raise SharedMemoryReadError ( shmloc + " has not been allocated. Requested " + str ( nbytes ) + " bytes." ) if readonly and preexisting != nbytes : raise SharedMemoryReadError ( "{} exists, but the allocation size ({} bytes) does not match the request ({} bytes)." . format ( shmloc , preexisting , nbytes ) ) if ( nbytes - preexisting ) > available : overallocated = nbytes - preexisting - available overpercent = ( 100 * overallocated / ( preexisting + available ) ) raise SharedMemoryAllocationError ( """
      Requested more memory than is available. 
      Shared Memory Location:  {}
      Shape:                   {}
      Requested Bytes:         {} 
      Available Bytes:         {} 
      Preexisting Bytes*:      {} 
      Overallocated Bytes*:    {} (+{:.2f}%)
      * Preexisting is only correct on linux systems that support /dev/shm/""" . format ( location , shape , nbytes , available , preexisting , overallocated , overpercent ) ) flags = 0 if readonly else O_CREAT size = 0 if readonly else int ( nbytes ) try : shared = posix_ipc . SharedMemory ( location , flags = flags , size = size ) array_like = mmap . mmap ( shared . fd , shared . size ) os . close ( shared . fd ) renderbuffer = np . ndarray ( buffer = array_like , dtype = dtype , shape = shape , order = order , ** kwargs ) except OSError as err : if err . errno == errno . ENOMEM : posix_ipc . unlink_shared_memory ( location ) raise renderbuffer . setflags ( write = ( not readonly ) ) return array_like , renderbuffer
Create a shared memory numpy array. Requires /dev/shm to exist.