Skip to content

Commit 26cdf1f

Browse files
committed
Make multiprocessing's shared memory use memoryview instead of raw pointer
1 parent 1a0df94 commit 26cdf1f

3 files changed

Lines changed: 5 additions & 26 deletions

File tree

Lib/multiprocessing/heap.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def malloc(self, size):
205205
self._lock.release()
206206

207207
#
208-
# Class representing a chunk of an mmap -- can be inherited
208+
# Class representing a chunk of an mmap -- can be inherited by child process
209209
#
210210

211211
class BufferWrapper(object):
@@ -218,11 +218,6 @@ def __init__(self, size):
218218
self._state = (block, size)
219219
Finalize(self, BufferWrapper._heap.free, args=(block,))
220220

221-
def get_address(self):
221+
def create_memoryview(self):
222222
(arena, start, stop), size = self._state
223-
address, length = _multiprocessing.address_of_buffer(arena.buffer)
224-
assert size <= length
225-
return address + start
226-
227-
def get_size(self):
228-
return self._state[1]
223+
return memoryview(arena.buffer)[start:start+size]

Lib/multiprocessing/sharedctypes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def rebuild_ctype(type_, wrapper, length):
132132
if length is not None:
133133
type_ = type_ * length
134134
ForkingPickler.register(type_, reduce_ctype)
135-
obj = type_.from_address(wrapper.get_address())
135+
buf = wrapper.create_memoryview()
136+
obj = type_.from_buffer(buf)
136137
obj._wrapper = wrapper
137138
return obj
138139

Modules/_multiprocessing/multiprocessing.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,6 @@ mp_SetError(PyObject *Type, int num)
5151
return NULL;
5252
}
5353

54-
55-
static PyObject*
56-
multiprocessing_address_of_buffer(PyObject *self, PyObject *obj)
57-
{
58-
void *buffer;
59-
Py_ssize_t buffer_len;
60-
61-
if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0)
62-
return NULL;
63-
64-
return Py_BuildValue("Nn",
65-
PyLong_FromVoidPtr(buffer), buffer_len);
66-
}
67-
6854
#ifdef MS_WINDOWS
6955
static PyObject *
7056
multiprocessing_closesocket(PyObject *self, PyObject *args)
@@ -137,9 +123,6 @@ multiprocessing_send(PyObject *self, PyObject *args)
137123
*/
138124

139125
static PyMethodDef module_methods[] = {
140-
{"address_of_buffer", multiprocessing_address_of_buffer, METH_O,
141-
"address_of_buffer(obj) -> int\n"
142-
"Return address of obj assuming obj supports buffer inteface"},
143126
#ifdef MS_WINDOWS
144127
{"closesocket", multiprocessing_closesocket, METH_VARARGS, ""},
145128
{"recv", multiprocessing_recv, METH_VARARGS, ""},

0 commit comments

Comments
 (0)