From 26cdf1fe5b4c42336a244ad8921bc660ff6819b4 Mon Sep 17 00:00:00 2001 From: Richard Oudkerk Date: Sat, 26 May 2012 22:09:59 +0100 Subject: [PATCH] Make multiprocessing's shared memory use memoryview instead of raw pointer --- Lib/multiprocessing/heap.py | 11 +++-------- Lib/multiprocessing/sharedctypes.py | 3 ++- Modules/_multiprocessing/multiprocessing.c | 17 ----------------- 3 files changed, 5 insertions(+), 26 deletions(-) diff --git a/Lib/multiprocessing/heap.py b/Lib/multiprocessing/heap.py index 311e402d352..4e93c12f815 100644 --- a/Lib/multiprocessing/heap.py +++ b/Lib/multiprocessing/heap.py @@ -205,7 +205,7 @@ class Heap(object): self._lock.release() # -# Class representing a chunk of an mmap -- can be inherited +# Class representing a chunk of an mmap -- can be inherited by child process # class BufferWrapper(object): @@ -218,11 +218,6 @@ class BufferWrapper(object): self._state = (block, size) Finalize(self, BufferWrapper._heap.free, args=(block,)) - def get_address(self): + def create_memoryview(self): (arena, start, stop), size = self._state - address, length = _multiprocessing.address_of_buffer(arena.buffer) - assert size <= length - return address + start - - def get_size(self): - return self._state[1] + return memoryview(arena.buffer)[start:start+size] diff --git a/Lib/multiprocessing/sharedctypes.py b/Lib/multiprocessing/sharedctypes.py index e473749981c..6dc160bf753 100644 --- a/Lib/multiprocessing/sharedctypes.py +++ b/Lib/multiprocessing/sharedctypes.py @@ -132,7 +132,8 @@ def rebuild_ctype(type_, wrapper, length): if length is not None: type_ = type_ * length ForkingPickler.register(type_, reduce_ctype) - obj = type_.from_address(wrapper.get_address()) + buf = wrapper.create_memoryview() + obj = type_.from_buffer(buf) obj._wrapper = wrapper return obj diff --git a/Modules/_multiprocessing/multiprocessing.c b/Modules/_multiprocessing/multiprocessing.c index d5593f0db8f..eb05c62b0d0 100644 --- a/Modules/_multiprocessing/multiprocessing.c +++ b/Modules/_multiprocessing/multiprocessing.c @@ -51,20 +51,6 @@ mp_SetError(PyObject *Type, int num) return NULL; } - -static PyObject* -multiprocessing_address_of_buffer(PyObject *self, PyObject *obj) -{ - void *buffer; - Py_ssize_t buffer_len; - - if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0) - return NULL; - - return Py_BuildValue("Nn", - PyLong_FromVoidPtr(buffer), buffer_len); -} - #ifdef MS_WINDOWS static PyObject * multiprocessing_closesocket(PyObject *self, PyObject *args) @@ -137,9 +123,6 @@ multiprocessing_send(PyObject *self, PyObject *args) */ static PyMethodDef module_methods[] = { - {"address_of_buffer", multiprocessing_address_of_buffer, METH_O, - "address_of_buffer(obj) -> int\n" - "Return address of obj assuming obj supports buffer inteface"}, #ifdef MS_WINDOWS {"closesocket", multiprocessing_closesocket, METH_VARARGS, ""}, {"recv", multiprocessing_recv, METH_VARARGS, ""},