pynng icon indicating copy to clipboard operation
pynng copied to clipboard

Support for memoryview?

Open ZisIsNotZis opened this issue 4 years ago • 2 comments

It would be nice to have memoryview supported to support zero-copying. Currently it seems that only bytes are supported which can't share memory with other objects

ZisIsNotZis avatar Dec 08 '20 09:12 ZisIsNotZis

Do you mean for the methods Socket.send() and Socket.asend()? (and similarly for the pipe and context methods?) Or did you have something else in mind?

codypiersall avatar Dec 17 '20 03:12 codypiersall

It is possible to use a memoryview for sending - currently, you need to pierce through the abstraction and use the ffi instance from pynng in your user code, to convert it into an object that is understood by cffi. For example like this:

from pynng import ffi
import numpy as np

data = np.zeros(128)  # or whatever - just needs to support the buffer protocol
mv = memoryview(data)
your_socket.send(ffi.from_buffer(mv))

Maybe this could be directly supported in the send method, by wrapping the object to send into a from_buffer if it is a memoryview? Or even directly support objects that implement the buffer protocol, so one can directly call your_socket.send(some_numpy_array)

sk1p avatar Oct 25 '21 15:10 sk1p