Shared memory structures
Project description
I’ve wondered why isn’t it easier to have multiple processes be able to have a shared memory space.
I’ve also wondered about how to pass shared memory definitions between processes.
Examples
import multiprocessing
from contextlib import suppress
from concurrent.futures import ProcessPoolExecutor, as_completed
from shmutils import MappedMemory, MapFlags, MapProtections
from shmutils.utils import cffiwrapper
from shmutils.lock import Lock
from shmutils.shm import shm_open, shm_unlink
def _increment_to(m, lock: Lock, value: cffiwrapper, limit: int):
"""
Returns the number of times we increments the number
"""
count = value[0]
num_incr = 0
while count < limit:
with lock:
count = value[0]
if count >= limit:
break
num_incr += 1
value[0] = count + 1
return num_incr
if __name__ == "__main__":
with suppress(FileNotFoundError):
shm_unlink("test-lock")
fd = shm_open("test-lock", "w+")
size = 4096 * 100
fd.truncate(size)
assert fd.size() == size
with MappedMemory(0, size, MapProtections.READ_WRITE, MapFlags.SHARED, fd) as m:
with ProcessPoolExecutor(mp_context=multiprocessing.get_context("fork")) as exe:
lock = Lock(m)
counter = m.new("size_t *", 0)
with lock:
result1 = exe.submit(_increment_to, m, lock, cffiwrapper(counter, m), 2_0000)
result2 = exe.submit(_increment_to, m, lock, cffiwrapper(counter, m), 2_0000)
tuple(as_completed((result1, result2)))
r1, r2 = result1.result(), result2.result()
assert (result1.exception(), result2.exception()) == (None, None)
assert counter[0] == 2_0000
assert r1 + r2 == 2_0000
Limitations
Toy allocator, does not synchronize writes between processes (only parent is expected to have allocated ahead of time).
spawn vs fork
Use “fork” multiprocessing method instead.
The “spawn” multiprocessing method is subject to ASLR and sometimes the kernel locates a child process starting much higher than our process. This has the effect of breaking the requirement for absolute pointers working.
However, it is observed that if a parent process manages to get a high enough memory page, the probability of the child process being able to mmap(2) the same address increases significantly.
import multiprocessing
from contextlib import suppress
from concurrent.futures import ProcessPoolExecutor, wait
from mmap import PAGESIZE
from shmutils import MappedMemory, MapFlags
from shmutils.mmap import munmap, round_to_page_size
from shmutils.shm import shm_open, ffi, shm_unlink
from shmutils.utils import cffiwrapper
# cffiwrapper - use to pickle/unpickle cffi objects between processes
def _set_data_to(value: cffiwrapper, to: int) -> int:
was = value[0]
for i in range(was, to):
value[0] = i
value[0] = to
return was
if __name__ == "__main__":
with suppress(FileNotFoundError):
shm_unlink("test-mmap-spawn")
with shm_open("test-mmap-spawn", "x+") as fd:
shared_size = round_to_page_size(1024 * 1024 * 1024)
fd.truncate(shared_size)
# Allocate a dummy 512 MiB blockrange
unused_space = MappedMemory(None, 512 * 1024 * 1024)
# write to the pages to ensure we're not being fooled
unused_space[len(unused_space) - PAGESIZE : len(unused_space) - PAGESIZE + 4] = b"sink"
# Calculate the last page in the unused space range
start_address: int = unused_space.abs_address_at[len(unused_space) - PAGESIZE]
# detach the unused space guts so we can free all bu the last page
raw_address, size = unused_space.detach()
# free all BUT the last page
munmap(raw_address, size - PAGESIZE)
del unused_space
# Prove our start address is the last page of the mostly freed range
# (our last page is still mapped.)
assert int(ffi.cast("uintptr_t", raw_address)) + size - PAGESIZE == start_address
with MappedMemory(
start_address, shared_size, flags=MapFlags.SHARED | MapFlags.FIXED, fd=fd
) as m:
with ProcessPoolExecutor(1, mp_context=multiprocessing.get_context("spawn")) as exe:
value = m.new("int64_t*", 1923)
assert value[0] == 1923
# The child process will now be able to mess with this counter
future = exe.submit(_set_data_to, cffiwrapper(value, m), 8900)
wait([future])
# And we can see the results both on the value in memory and from the
# return
assert future.done() and not future.exception()
assert (future.result(), value[0]) == (1923, 8900)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for shmutils-0.0.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9960ab109c6c31913aff71212f240145e31468c586ee5f5bbc982a80957ab3fd |
|
MD5 | 127be6c1ba547b165f117553adad406f |
|
BLAKE2b-256 | 1b98ccc2f32e0560fec949bf883c0c0531c96970e6d7afa8a3b4741ed1ae64cb |
Hashes for shmutils-0.0.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 146e1f8c4dbf462c1403677b82d4ec44ecf79b1006f829b9ef075be07c0c9967 |
|
MD5 | 2e5e2039bd834d16aeb8b2013149fc67 |
|
BLAKE2b-256 | a038e2ba67f7bffbeba1d37d2510cfab103767cfb4384edce90f634f5c08646c |
Hashes for shmutils-0.0.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06ec45aa09f97de5bce97dee1c451341fbcbda86dd6608d614a4634d0d5ed28b |
|
MD5 | 7093ab16e63dc5605c9f193e6d52e60c |
|
BLAKE2b-256 | 8b46c338f3b12fe259d15f5b139a2da7137b3f2bbc50aecd389eca9557e5a63a |
Hashes for shmutils-0.0.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a7869b75d5e53c40fcf3e8a664a6e90957997fc747b91391ca236667cd8fc00 |
|
MD5 | 51079a93627586b6870c6be536bd8778 |
|
BLAKE2b-256 | 8b3091ac789efcaa6d2a4c09a41af6fbde2baf286eeaa1c08a35927b54603bdc |
Hashes for shmutils-0.0.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f4228c16319748bd9af3366f9be7b9c3fc51d99862067ff9ca214b0bcd5eeaa |
|
MD5 | bc526918a5ddb7a2b7c8ad8032c15f5d |
|
BLAKE2b-256 | 8bd11d2e2dfbab10ee30f975f58429044c7b59b8b52ab3d2f3078805abfd1b78 |
Hashes for shmutils-0.0.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc6ae46270ac68c63de990c57f9db2105d6f93bd707aa93ba40407fa2c5f5e40 |
|
MD5 | d341f50bdc932f43cbc792397dab0c05 |
|
BLAKE2b-256 | 0b95fe7ebf20e54ff789c3366d1f48c4a1e501de81a53e35306df0ddf753efab |
Hashes for shmutils-0.0.0-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9259af8aea8305c4ff96879be1b1636b2ed14265ab8e2a368ec46f368ff611a |
|
MD5 | 2923a866d8a851b8982c8a0ec4b02524 |
|
BLAKE2b-256 | 887fe9fe4c3fa88dbf56fb07598dd04467caef9fbb8eb55863fd7d64bef1332b |
Hashes for shmutils-0.0.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7de8a8a82a61d973845c1ecd6549d4962a4af38609d593f6fb963e2471490bfa |
|
MD5 | b90b73a5fe7711d6d4f0967316c27403 |
|
BLAKE2b-256 | 6ede1eb41f23d07ee31f5e9e867f7e64a135fe624ed07825e6d85660f527bffb |
Hashes for shmutils-0.0.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06f3913004f8e631e8c79ca3894d8e7bd34dfd5a5d0eb659c000b2dd80f46955 |
|
MD5 | 18365d0d1bd0aecbe5b833081b936caf |
|
BLAKE2b-256 | 52d5860b73245d488db27c8bc4e5dcc73c13cd2d5821d6f432f0059cbdf45563 |
Hashes for shmutils-0.0.0-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 076012246f57912f69e64d44d692994eaef673f968a64df393a857f0444faac8 |
|
MD5 | 4a162f38a4a1d2c5cc932f59fc295fec |
|
BLAKE2b-256 | f3ffd72c297882bc3bfda4e0469a60bd649ebccb04478df75e14778407d8fb21 |
Hashes for shmutils-0.0.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ba78d1111f713407ebce921ba68fea5d49e5172b2685a29c37129b3aec7933a |
|
MD5 | 6712ad7c9c83b00de578d19cb267c519 |
|
BLAKE2b-256 | 3bd7c271016527050ce6df23d0b45ccac0152c84a039c05cdcc2dac567780f90 |
Hashes for shmutils-0.0.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d218f2c49e706d0525340fd4a8d4d40e1f3c28d263fa08e4f0d643b8e74f409 |
|
MD5 | 72d2ce821a5cc10f6bad9d86f9622cb7 |
|
BLAKE2b-256 | bd74b5b590c1a82fa6b7a52f08665c82ba673741819c75d11ba8bfbc737d9469 |