Skip to content

Instantly share code, notes, and snippets.

@mjs
Created January 18, 2023 09:18
Show Gist options
  • Save mjs/79d221842fb5143869c1245ac2a2bce9 to your computer and use it in GitHub Desktop.
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
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'))
@mjs
Copy link
Author

mjs commented Jan 18, 2023

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 :)

@mjs
Copy link
Author

mjs commented Jan 18, 2023

The mmap could also be file backed if persistence was required (remove MAP_ANONYMOUS and pass a fileno).

@mullenkamp
Copy link

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