code: def persist_shm(model, dir=None):
      """
      Persist a model using binpickle.
  
      Args:
          model: The model to persist.
          dir: The temporary directory for persisting the model object.
  
      Returns:
          PersistedModel: The persisted object.
      """
  
      buffers = []
  
      with sharing_mode():
          data = pickle.dumps(
              model, protocol=5, buffer_callback=buffers.append
          )
  
      total_size = sum(memoryview(b).nbytes for b in buffers)
      _log.info(
          "serialized %s to %d pickle bytes with %d buffers of %d bytes",
          model,
          len(data),
          len(buffers),
          total_size,
      )
  
      if buffers:
          # blit the buffers to the SHM block
          _log.debug("preparing to share %d buffers", len(buffers))
          memory = shm.SharedMemory(create=True, size=total_size)
          cur_offset = 0
          blocks = []
          for i, buf in enumerate(buffers):
              ba = buf.raw()
              blen = ba.nbytes
              bend = cur_offset + blen
              _log.debug(
                  "saving %d bytes in buffer %d/%d",
                  blen,
                  i + 1,
                  len(buffers),
              )
              memory.buf[cur_offset:bend] = ba
              blocks.append((cur_offset, bend))
              cur_offset = bend
      else:
          memory = None
          blocks = []
  
      return SHMPersisted(data, memory, blocks)
  
file_path: lkpy/lenskit/sharing/shm.py
imports: import sys
  import logging
  import pickle
  from .None import sharing_mode, PersistedModel
  import multiprocessing.shared_memory
label_desc: Use image node_id 1 for calling a global function with example usage: persist_shm(model, dir) and returns: SHMPersisted