python / cpython

The Python programming language
https://www.python.org
Other
63.33k stars 30.31k forks source link

create multiprocessing.SharedMemory by pointing to existing memoryview #83948

Open edbe8e45-68ad-41d5-8021-29d38e2f9712 opened 4 years ago

edbe8e45-68ad-41d5-8021-29d38e2f9712 commented 4 years ago
BPO 39767

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['3.8', 'library', 'performance'] title = 'create multiprocessing.SharedMemory by pointing to existing memoryview' updated_at = user = 'https://bugs.python.org/DariuszTrawinski' ``` bugs.python.org fields: ```python activity = actor = 'petr.viktorin' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'Dariusz Trawinski' dependencies = [] files = [] hgrepos = [] issue_num = 39767 keywords = [] message_count = 1.0 messages = ['362754'] nosy_count = 1.0 nosy_names = ['Dariusz Trawinski'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'performance' url = 'https://bugs.python.org/issue39767' versions = ['Python 3.8'] ```

edbe8e45-68ad-41d5-8021-29d38e2f9712 commented 4 years ago

Currently, in order to share numpy array between processes via multiprocessing.SharedMemory object, it is required to copy the memory content with: input = np.ones((1,10,10,10)) shm = shared_memory.SharedMemory(create=True, size=input.nbytes) write_array = np.ndarray(input.shape, dtype=input.dtype,buffer=shm.buf) write_array1[:] = input[:] In result the original numpy array is duplicated in RAM. It also adds extra cpu cycles to copy the content.

I would like to recommend adding an option to create shared memory object by pointing it to existing memoryview object, beside current method of using shared memory name. Is that doable?