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

Arnaud Loonstra arnaud at sphaero.org
Wed Jun 3 17:25:29 CEST 2015


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



More information about the zeromq-dev mailing list