.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/memmap/plot_mman.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite or Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_memmap_plot_mman.py: Memory-Mapping Showcase – Basic / Medium / Advanced ==================================================== This gallery example demonstrates the :py:class:`~scikitplot.memmap.MemoryMap` module at three levels of complexity. * **Basic** – anonymous mapping: write bytes, read them back, verify. * **Medium** – file-backed mapping with ``msync``; visualise the on-disk byte layout with Matplotlib. * **Advanced**– zero-copy NumPy view (``as_numpy_array``), dynamic protection changes via ``mprotect``, and a micro-benchmark comparing ``read()`` vs the NumPy view. All three sections are self-contained; you can run just the one you need. Prerequisites ------------- * The ``mman`` Cython extension must be compiled and importable. * NumPy and Matplotlib are required for the Medium and Advanced sections. .. GENERATED FROM PYTHON SOURCE LINES 30-33 --------------------------------------------------------------------------- Imports --------------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-50 .. code-block:: Python import os import struct import tempfile import time import numpy as np import matplotlib.pyplot as plt from scikitplot.memmap import ( MemoryMap, PROT_READ, PROT_WRITE, MAP_SHARED, MS_SYNC, ) .. GENERATED FROM PYTHON SOURCE LINES 51-56 =========================================================================== BASIC – anonymous mapping: write / read / verify =========================================================================== An *anonymous* mapping is backed by RAM only — no file on disk. This is the simplest way to get a writable memory region managed by the kernel. .. GENERATED FROM PYTHON SOURCE LINES 56-82 .. code-block:: Python # --- 1. allocate 4 KB anonymous region ------------------------------------ PAGE: int = 4096 # one page on most platforms with MemoryMap.create_anonymous(PAGE, PROT_READ | PROT_WRITE) as m: # --- 2. write a greeting ---------------------------------------------- message: bytes = b"Hello from mman!" n_written: int = m.write(message) print(f"[BASIC] wrote {n_written} bytes") # --- 3. read it back and verify --------------------------------------- read_back: bytes = m.read(n_written) assert read_back == message, f"mismatch: {read_back!r}" print(f"[BASIC] read back: {read_back}") # --- 4. write at an offset -------------------------------------------- offset_msg: bytes = b"offset data" m.write(offset_msg, offset=256) assert m.read(len(offset_msg), offset=256) == offset_msg print(f"[BASIC] offset write/read OK (offset=256)") # --- 5. report page size ---------------------------------------------- print(f"[BASIC] OS page size = {m.page_size} bytes") print("[BASIC] mapping closed automatically by context manager.\n") .. rst-class:: sphx-glr-script-out .. code-block:: none [BASIC] wrote 16 bytes [BASIC] read back: b'Hello from mman!' [BASIC] offset write/read OK (offset=256) [BASIC] OS page size = 4096 bytes [BASIC] mapping closed automatically by context manager. .. GENERATED FROM PYTHON SOURCE LINES 83-89 =========================================================================== MEDIUM – file-backed mapping: write, sync, read back, visualise =========================================================================== A *file-backed* mapping lets you read/write a file as if it were an array in RAM. With ``MAP_SHARED`` + ``msync`` every modification is flushed to disk. We then re-read the raw file to prove the bytes made it. .. GENERATED FROM PYTHON SOURCE LINES 89-170 .. code-block:: Python # -------------------------------------------------------------------------- # helpers # -------------------------------------------------------------------------- def _pack_row(index: int, value: float) -> bytes: """Pack one 12-byte record: 4-byte little-endian int + 8-byte double.""" return struct.pack("` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mman.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_mman.zip ` .. include:: plot_mman.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_