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

MinRK benjaminrk at gmail.com
Sat Jun 6 01:52:35 CEST 2015


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.

-MinRK
​

On Thu, Jun 4, 2015 at 1:59 AM, Arnaud Loonstra <arnaud at sphaero.org> wrote:

> On 2015-06-03 17:25, Arnaud Loonstra wrote:
> > On 2015-06-03 10:20, Arnaud Loonstra wrote:
> >> I read here: http://zeromq.github.io/pyzmq/serialization.html PyZMQ
> >> being able to pass an object without copying it.
> >> I guess this holds only for the serialised copy of the data..? In
> >> theory the object can be passed as a reference thus preventing a
> >> copy.
> >>
> >> Suppose I have an image as a python object which is produced in a
> >> thread. Now an other thread wants to display it. The only transport
> >> which would be able to do this is 'inproc'. However the send method
> >> of
> >> PyZMQ needs an object supporting the Buffer interface (I guess to
> >> copy
> >> the data). How would I be able to pass the image to the other thread
> >> without copying it?
> >>
> >> Rg,
> >>
> >> Arnaud
> >
> > I've found a really nasty but simple way to accomplish this
> >
> >>>> from PIL import Image
> >>>> import ctypes
> >>>> a = Image.new("RGB", (200,600), (255,255,55))
> >>>> a
> > <PIL.Image.Image image mode=RGB size=200x600 at 0x7F7DEA624B70>
> >>>> id(a)
> > 140178779949936
> >>>> ctypes.cast(id(a), ctypes.py_object).value
> > <PIL.Image.Image image mode=RGB size=200x600 at 0x7F7DEA624B70>
> >>>> ctypes.cast(140178779949936, ctypes.py_object).value
> > <PIL.Image.Image image mode=RGB size=200x600 at 0x7F7DEA624B70>
> >>>>
> >
> > So I'm using the id() method of Python to get the pointer address
> > (integer) of the object. I send this value over the socket wher on
> > receiving I cast it bak to a python object.
> >
> > It's working. I don't if this will run into trouble with garbage
> > collection but so far so good. It probably only works in CPython.
> >
> > Rg,
> >
> > Arnaud
>
> If anybody's interested I created a proof of concept example. It kind
> of comes with a "do not try this at home" warning.
>
> https://gist.github.com/sphaero/3ff5a72e699d828306ec
>
> If anybody knows other approaches I'm very interested!
>
> Rg,
>
> Arnaud
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20150605/e3edf43c/attachment.htm>


More information about the zeromq-dev mailing list