from multiprocessing import Value
from multiprocessing.sharedctypes import RawArray
from ctypes import addressof, c_char
array = RawArray(c_char, b'shirt')
array_length = array._length_
result_address = Value('i', 0)
result_address.value = addressof(array)
print((array._type_*array_length).from_address(result_address.value).value)
In one process I pass result_address of type multiprocessing.Value to a child process, which then creates an array in shared memory and writes its address back to result_address. Basically I want to share a bytestring created in child process with the parent process without using multiprocessing.Manager as it's only a bytestring and it can (?) be managed in shared memory.
Also this (array._type_*array_length).from_address() maybe creates the array in memory again, how do I just read the c array in python string without creating another array?
The code even does not work in single process and produces Segmentation fault.
Valuecan hold a 4-byte pointer by using an'L'typecode instead of'i'who's minimum size only 2 bytes. You may need 8 bytes for 64-bit systems.