[zeromq-dev] czmq python binding Zpoller

Johan Philips johan.philips at kuleuven.be
Wed Nov 11 00:18:01 CET 2015



On 2015-11-11 01:35, Arnaud Loonstra wrote:
> On 2015-11-05 12:20, Arnaud Loonstra wrote:
>> On 2015-11-05 02:50, Johan Philips wrote:
>>> How can I get the actual socket out of it, because Zpoller seems to
>>> return a c_void_p which I cannot compare to my Zsock object I added
>>> to
>>> the poller.
>>>
>>> Minimal example using zyre nodes:
>>> Python 2.7.9 (default, Apr  2 2015, 15:33:21)
>>> [GCC 4.9.2] on linux2
>>> Type "help", "copyright", "credits" or "license" for more
>>> information.
>>>   >>> from zyre import Zyre
>>>   >>> from czmq import Zsock, Zpoller
>>>   >>> n1 = Zyre("n1")
>>>   >>> n2 = Zyre("n2")
>>>   >>> n1.start()
>>> 0
>>>   >>> n2.start()
>>> 0
>>>   >>> poller = Zpoller(n1.socket(),n2.socket(), None)
>>>   >>> sock = poller.wait(100)
>>>   >>> print sock
>>> c_void_p(27301232)
>>>   >>> print n1.socket(), n2.socket()
>>> <czmq.Zsock object at 0x7fbd0968cfd0> <czmq.Zsock object at
>>> 0x7fbd0968cfd0>
>>>   >>>
>>>
>>> Do I need to add cast or POINTER or is there a more convenient way?
>>> Zsock(result, True) does not work since it will create a new object.
>>>
>>> Thanks!
>>>
>>> Johan
>>
>> I haven't found the best approach yet. First of all you can use
>> ZyreEvents. See the test.py file for an example.
>> I've used the zyre binding with PyZMQ. I had to wrap the binding to
>> cast the pointer to a PyZMQ socket object. I don't have the code here
>> so
>> can't provide how it is done. I might have posted it on this ML
>> though.
>>
>> I haven't used CZMQ for this.... yet. I'd need to look into this.
>>
>> Perhaps some else knows a better way because indeed it is essential.
>
> I don't know if it helps but this what I've used to acquire a pyZMQ
> socket object from the python bindings.
>
> For example when creating a Zyre instance you can get the socket like
> this:
>
>> z1 = Zyre(b't1')
>> s = z1.socket()
>
> However that will be a <czmq.Zsock object at 0x7f61db9ce588>
>
> You can then use the resolve method to acquire a pointer to the socket.
> This pointer can be fed to the pyZMQ Socket constructor to turn into a
> pyZMQ Socket object:
>
>> zs = zmq.Socket(shadow=s.resolve().value)
>
> I can use this socket with a zmq.Poller
>

Thanks! Didn't know about the shadow attribute. However, polling seems 
to be fine with zmq.Poller(z1.socket(), NULL). The check to see which 
poll item received data is the issue.

E.g.

which = Zpoller(z1.socket(), z2.socket(), NULL)
if(which == z1.socket()) # This does not work


> Rg,
>
> Arnaud
> _______________________________________________
> 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