0
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.

5
  • I'm guessing it's probably due to what is mentioned in the Note here in the documentation. Commented Sep 26, 2016 at 12:40
  • @martineau They say only something about pointers. I do save an address location in shared memory, but its an address of an array also in shared memory. Nevertheless it does not work in a single process. Commented Sep 26, 2016 at 12:51
  • Try making sure the Value can 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. Commented Sep 26, 2016 at 13:12
  • @martineau thanks now it works in single process, but as you suggested it does not work when multiprocessing. Although I don't understand why, when I allocate the Array in shared memory. Commented Sep 26, 2016 at 13:48
  • If it worked single process by making it 4-bytes, try making it 8-bytes and see if that helps the multiprocessing. Seems reasonable that addressing shared memory might require a larger address in order to be accessible from any of them. Commented Sep 26, 2016 at 16:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.