[zeromq-dev] Passing a python object (PyZMQ)

Min RK benjaminrk at gmail.com
Mon Jun 8 18:05:08 CEST 2015



> On Jun 8, 2015, at 01:35, Arnaud Loonstra <arnaud at sphaero.org> wrote:
> 
>> On 06/06/2015 01:52 AM, MinRK wrote:
>> Without using ctypes, you could pass the objects through a namespace:
>> 
>> |# shared namespace
>> ns = {}
>> 
>> # sender
>> ns[id(obj)] = obj
>> pipe_out.send(struct.pack(b'Q',1))
>> 
>> # receiver
>> id_bytes = pipe_in.recv()
>> obj_id = struct.unpack(b'Q', id_bytes)[0]
>> obj = ns.pop(obj_id)
>> |
>> 
>> The ctypes cast approach doesn’t hold a reference to the object while
>> it’s in transit, so it’s possible for the restoration to fail if the
>> object has been garbage collected in between send/recv.
> 
> You're right I ended up doing something very similar to workaround early 
> garbage collection but this is much nicer. However it only works between 
> a producer and consumer thread, not more. So every producer thread needs 
> its own namespace.

I'm not sure why. You should only need one namespace per process.

-MinRK

> 
> Rg,
> 
> Arnaud
> -- 
> w: http://www.sphaero.org
> t: http://twitter.com/sphaero
> g: http://github.com/sphaero
> i: freenode: sphaero_z25
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev



More information about the zeromq-dev mailing list