Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for recv_into #95

Open
sk1p opened this issue Oct 25, 2021 · 1 comment
Open

Support for recv_into #95

sk1p opened this issue Oct 25, 2021 · 1 comment

Comments

@sk1p
Copy link

sk1p commented Oct 25, 2021

My use case was sending numpy arrays via nng, where it really makes sense to reduce the number of allocations and copies that need to be performed.

Reading the code, I saw that there is already the general idea to support a recv_into operation on sockets. I built a prototype that looks like this, based on the normal read method:

def recv_into(s, buffer):
    """Receive data on the socket s into buffer
    If the request times out the exception
    :class:`pynng.Timeout` is raised.  If the socket cannot perform that
    operation (e.g., a :class:`Pub0`, which can only
    :meth:`~Socket.send`), the exception :class:`pynng.NotSupported`
    is raised.
    """
    from pynng import ffi, lib
    from pynng.nng import check_err
    # we DON'T set lib.NNG_FLAG_ALLOC here, because we want to give
    # our own recv buffer
    flags = 0
    size_t = ffi.new('size_t *')
    size_t[0] = len(buffer)
    ret = lib.nng_recv(s.socket, buffer, size_t, flags)
    check_err(ret)
    assert size_t[0] == len(buffer)

Used like this:

recv_mem = np.zeros(arr_shape, dtype=np.float32)
recv_buf = ffi.from_buffer(
    recv_mem,
    require_writable=True,
)
recv_into(your_socket, recv_buf)
# the received data is now available in `recv_mem`

If there is interest, I could convert this into a PR including test cases etc. and add proper support for returning the part of the buffer that was written to etc. (this prototype was only used for evaluation purposes / benchmarking). Let me know if anyone is interested!

@aqc-carlodri
Copy link

Thank you, yes I would definitely be interested in this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants