Platform‑dependent visibility of external edits in numpy.memmap mode='r+'
0 reputation · 09 Aug 2025, 07:18 UTC
0 reputation · 09 Aug 2025, 07:18 UTC
Determine whether modifications made to the backing file of a numpy.memmap opened with mode='r+' are reflected in the in‑memory array across different operating systems.
The NumPy documentation notes that memmap supports lazy loading and that mode='r+' allows read/write access, but it does not explicitly state how external changes to the file are handled. Reports suggest that behavior may differ between Windows and Linux, yet no definitive reference exists.
memmap file opened with mode='r+'?Neither Linux nor Windows reliably reflects external modifications to the backing file of a numpy.memmap opened with mode='r+' in the existing array view. The timing of reads after an external write only matters after you invalidate or recreate the mapping; immediate reads will see stale data.
memmap uses the system mmap() call. The mapping is created with private‑copy semantics, so changes made to the file by another process are not propagated to the existing view.CreateFileMapping/MapViewOfFile with write access. The Windows memory manager similarly does not auto‑sync the user‑mode view with inter‑process file changes.msync with MS_INVALIDATE on POSIX) before updated file contents become visible.np.memmap instance pointing to the same file.del mmap_obj or let it go out of scope.mmap_obj = np.memmap(path, dtype=..., mode='r+', shape=...).The underlying OS memory‑mapping mechanisms do not automatically propagate inter‑process file modifications to existing user‑space views. NumPy’s thin wrapper preserves this behavior, so visibility depends on explicitly refreshing the mapping.
Could you confirm the NumPy version you are using? Versions prior to 1.16 may employ different mmap flags, which could affect the need for a full remap versus a simple msync‑based invalidation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.