How do I give child processes access to data in shared memory if the data is only available after the child processes have been spawned (using multiprocessing.Process)?
I am aware of multiprocessing.sharedctypes.RawArray, but I can't figure out how to give my child processes access to a RawArray
that is created after the processes have already started.
The data is generated by the parent process, and the amount of data is not known in advance.
If not for the GIL I'd be using threading instead which will make this task a little simpler. Using a non-CPython implementation is not an option.
Looking under the hood of muliprocessing.sharedctypes, it looks like shared ctype objects are allocated using mmap
ed memory.
So this question really boils down to: Can a child process access an anonymously mapped memory if mmap()
was called by the parent after the child process was spawned?
That's somewhat in the vein of what's being asked in this question, except that in my case the caller of mmap()
is the parent process and not the child process.
(Solved)
I created my own version of RawArray
that uses shm_open()
under the hood. The resulting shared ctypes array can be shared with any process as long as the identifier (tag
) matches.
See this answer for details and an example.
Disclaimer: I am the author of the question.
I eventually used the posix_ipc module to create my own version of RawArray. I used mainly
posix_ipc.SharedMemory
which callsshm_open()
under the hood.My implementation (
ShmemRawArray
) exposes the same functionality asRawArray
but required two additional parameters - atag
to uniquely identify the shared memory region, and acreate
flag to determine if we should be created a new shared memory segment or attach to an existing one.Here's a copy if anyone's interested: https://gist.github.com/1222327
Usage notes:
typecode_or_type
andsize_or_initializer
) should work the same as withRawArray
.tag
matches.ShmemRawArray(..., create=True)
) is deletedtag
that currently exists will raise anExistentialError
tag
that doesn't exist (or one that has been unlinked) will also raise anExistentialError
A SSCCE (Short, Self Contained, Compilable Example) showing it in action.
Running this results in the following output:
I think you are looking for mmap module
concerning the serializiation of data this question answer of course if you hope to avoid copy I have not the solution
EDIT
in fact you can use the non stdlib _mutliprocessing module in CPython 3.2 to have the address of the mmap object and use it with from_address of a ctypes object it is what in fact what does RawArray in fact of course you should not try to resize the mmap object as the address of mmap may change in this case
to expose the memory after the child is created you have to map your memory with a real file that is calling
Your problem sounds like a perfect fit for the
posix_ipc
orsysv_ipc
modules, which expose either the POSIX or SysV APIs for shared memory, semaphores, and message queues. The feature matrix there includes excellent advice for picking amongst the modules he provides.The problem with anonymous
mmap(2)
areas is that you cannot easily share them with other processes -- if they were file-backed, it'd be easy, but if you don't actually need the file for anything else, it feels silly. You could use theCLONE_VM
flag to theclone(2)
system call if this were in C, but I wouldn't want to try using it with a language interpreter that probably makes assumptions about memory safety. (It'd be a little dangerous even in C, as maintenance programmers five years from now might also be shocked by theCLONE_VM
behavior.)But the SysV and newer POSIX shared memory mappings allow even unrelated processes to attach and detach from shared memory by identifier, so all you need to do is share the identifier from the processes that create the mappings with the processes that consume the mappings, and then when you manipulate data within the mappings, they are available to all processes simultaneously without any additional parsing overhead. The
shm_open(3)
function returns anint
that is used as a file descriptor in later calls toftruncate(2)
and thenmmap(2)
, so other processes can use the shared memory segment without a file being created in the filesystem -- and this memory will persist even if all processes using it have exited. (A little strange for Unix, perhaps, but it is flexible.)