[zeromq-dev] router/dealer doubts

andrea crotti andrea.crotti.0 at gmail.com
Tue Aug 21 17:17:08 CEST 2012


2012/8/21 Pieter Hintjens <ph at imatix.com>:
> On Tue, Aug 21, 2012 at 1:41 PM, Chuck Remes <lists at chuckremes.com> wrote:
>
>> http://www.zeromq.org/tutorials:xreq-and-xrep
>> Before we renamed them, DEALER was XREQ and ROUTER was XREP so all of the same information still applies.
>
> I've moved that page to
> http://www.zeromq.org/tutorials:dealer-and-router and fixed up the
> text.
>
> #itsawiki
>
> -Pieter
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev


I've read everything I found but still far from being really clear..

So I found the router examples (rrserver/rrclient) and so one, and I
can write a broker like this:

def broker():
    context = zmq.Context()
    frontend = context.socket(zmq.ROUTER)
    frontend.bind(FRONT_PORT)

    backend = context.socket(zmq.DEALER)
    backend.bind(BACK_PORT)

    poll = zmq.Poller()
    poll.register(frontend, zmq.POLLIN)
    poll.register(backend, zmq.POLLIN)

    while True:
        socks = dict(poll.poll())
        if socks.get(frontend) == zmq.POLLIN:
            msg = frontend.recv()
            more = frontend.getsockopt(zmq.RCVMORE)
            if more:
                backend.send(msg, zmq.SNDMORE)
            else:
                backend.send(msg)

        if socks.get(backend) == zmq.POLLIN:
            message = backend.recv()
            more = backend.getsockopt(zmq.RCVMORE)
            if more:
                frontend.send(message, zmq.SNDMORE)
            else:
                frontend.send(message)

And here the identity of the sender is handled automatically, and how
should I fetch it in theory?


And what if I wanted to have only the dealer or only the router?
I tried with something like this:

def router():
    context = zmq.Context()
    frontend = context.socket(zmq.ROUTER)
    frontend.bind(FRONT_PORT)
    backend = context.socket(zmq.REQ)
    backend.connect(BACK_PORT)

    while True:
        _id = frontend.recv()
        frontend.recv()
        query = frontend.recv()
        print("received ", _id, query)
        #
        backend.send(query)
        ans = backend.recv()
        print("received answer", ans)
        frontend.send(_id)
        frontend.send('')
        frontend.send(ans)

and now I actually receive the message in three parts, with the id and
an empty message, but does it make sense to forward it like this to the server?



More information about the zeromq-dev mailing list