import mmap
import pickle


def load(path):
    """Load serialized object with out-of-band data from path based on zero-copy shared memory.

    Parameters
    ----------
    path : pathlib.Path
        Folder used to save serialized data with serialize(). Usually a folder /dev/shm

    """
    num_buffers = len(list(path.iterdir())) - 1  # exclude meta.idx
    buffers = []
    for idx in range(num_buffers):
        f = open(path / f'{idx}.bin', 'rb')
        buffers.append(mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ))
    with open(path / 'meta.pkl', 'rb') as f:
        return pickle.load(f, buffers=buffers)