[zeromq-dev] zproject python binding mixing with pyZMQ

Arnaud Loonstra arnaud at sphaero.org
Thu Jun 4 13:03:13 CEST 2015


On 2015-06-02 13:56, Arnaud Loonstra wrote:
> On 2015-05-03 16:25, Arnaud Loonstra wrote:
>> Hi all,
>>
>> I'm testing the new python bindings from zproject. As a test I was
>> trying the Zyre bindings. In Zyre you can retrieve the socket using
>> socket() in case you want to add it to a poller. However the socket
>> method returns a  <class 'zyre.LP_zsock_t'> type which not hashable
>> and
>> probably won't be understood by pyZMQ.
>>
>> Any ideas how to deal with this? I think it would make sense if the
>> binding is inter operable with PyZMQ.
>>
>
> To make this work the generator needs to address for the socket types
> beginning with:
> --- a/bindings/python/zyre.py
> +++ b/bindings/python/zyre.py
> @@ -45,6 +45,14 @@ zyre_p = POINTER(zyre_t)
>
>   class zsock_t(Structure):
>       pass # Empty - only for type checking
> +zsock_t._fields_=[
> +        ("tag", c_uint),
> +        ("handle", c_void_p),
> +        ("endpoint", c_char_p),
> +        ("cache", c_char_p),
> +        ("type", c_int),
> +        ("cache_size", c_size_t)
> +    ]
>   zsock_p = POINTER(zsock_t)
>
> The handle is an actual zmq socket. We only need to get to it's file
> descriptor to be able to use it in a select/poll.
>
> Perphaps the handle can be converted to a PyZMQ socket type?

I might have some proof of concept code. I changed this to zyre.py:

--- a/bindings/python/zyre.py
+++ b/bindings/python/zyre.py
@@ -44,7 +44,14 @@ class zyre_t(Structure):
  zyre_p = POINTER(zyre_t)

  class zsock_t(Structure):
-    pass # Empty - only for type checking
+    _fields_=[
+        ("tag", c_uint),
+        ("handle", c_void_p),
+        ("endpoint", c_char_p),
+        ("cache", c_char_p),
+        ("type", c_int),
+        ("cache_size", c_size_t)
+    ]
  zsock_p = POINTER(zsock_t)

This simple example shows getting the zyre socket as a PyZMQ socket

>>> from zyre import Zyre
>>> import zmq
>>> z1 = Zyre('t1')
>>> s = z1.socket()
>>> print(s)
<zyre.LP_zsock_t object at 0x7fd6e3439170>
>>> print(s.contents.endpoint)
inproc://pipe-ee96-5a9d
>>> sock_pointer = s.contents.handle
>>> s2 = zmq.Socket(shadow=sock_pointer)
>>> print(s)
<zmq.sugar.socket.Socket object at 0x7fd6e3473390>
>>> print(s2.closed)
False
>>> print(s2.get_hwm())
1000
>>> print(s2.underlying)
44483568
>>> print(s2.get(zmq.FD))
9

I'm not sure whether this is interchangeable with CFFI and Cython

Rg,

Arnaud



More information about the zeromq-dev mailing list