[zeromq-dev] Non-copying recv

Brian Granger ellisonbg at gmail.com
Fri May 21 17:19:46 CEST 2010

On Thu, May 20, 2010 at 10:43 PM, Martin Sustrik <sustrik at 250bpm.com> wrote:
> Hi Brian,
>> We are putting the final touches to the non-copying send/recv in the
>> Python bindings and I have a question about the non-copy recv.
>> The following will now work in a non-copying manner:
>>>>> msg = s.recv(copy=False)
>>>>> type(msg)
>> 'Message'
>>>>> s2.send(msg)
>> But in this simple case the contents of the msg are not used in any
>> way by the app.  What if the app wants to use the contents of the
>> Message and even change it, but not make a copy?  An example would be
>> to pass the msg buffer to an array, which is then modified in place:
>>>>> msg = s.recv(copy=False)
>>>>> a = new_array(msg)
>>>>> a[0,0] = 4.0  # inplace modification
>> It seems like the array object would need to take ownership of the
>> buffer and that 0MQ couldn't deallocate the buffer.
>> Is this type of thing possible?  I could simple increase the 0MQ
>> ref-count to prevent 0MQ from freeing the message, but I am not sure
>> that makes sense.
> When you receive a message the ownership is passed to you. You have a
> message wih refcount of 1. It gets deallocated when you call zmq_msg_close.
> You can change the content, you can copy it. Obviously, once you copy it,
> changing the content would be visible on every copy.

Great, I can then use this logic to pass the Message contents to a NumPy array.



> Martin

Brian E. Granger, Ph.D.
Assistant Professor of Physics
Cal Poly State University, San Luis Obispo
bgranger at calpoly.edu
ellisonbg at gmail.com

More information about the zeromq-dev mailing list