Created
January 18, 2023 09:18
-
-
Save mjs/79d221842fb5143869c1245ac2a2bce9 to your computer and use it in GitHub Desktop.
Example of how to use a anonymous mmap block with the multiprocessing module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Pool | |
import mmap | |
mm = mmap.mmap(-1, 12, flags=mmap.MAP_SHARED|mmap.MAP_ANONYMOUS) | |
n = mm.write(b"lots of data") | |
mm.seek(0) | |
print(mm.read().decode('ascii')) | |
mm.seek(0) | |
def f(x): | |
offset, char = x | |
mm[offset:offset+1] = str(char).encode('ascii') | |
with Pool(3) as p: | |
p.map(f, [(0, "f"), (1, "o"), (2, "o")]) | |
print(mm.read().decode('ascii')) |
The mmap could also be file backed if persistence was required (remove MAP_ANONYMOUS and pass a fileno).
Oh! Very nice! I guess that article was wrong. I'd much rather use mmap than the shared_memory module. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The mmap block is shared by the parent and children processes and is writeable by all. No data needs to be serialised in order to be sent between processes. Synchronisation of access is left as an exercise for the reader :)