On 13-03-26 11:07 AM Pieter Hintjens <[email protected]> wrote to ZeroMQ development list <[email protected]> :

* 0MQ sockets manage a set of "pipes" to connected peers
* A pipe is created either on a connect (at once) or when an incoming
connection arrives
* No pipe means messages can't be sent... (the mute state)

So if you bind a socket and then connect to it, there will be an
arbitrary period when you can't send. Using ZMQ_MANDATORY means you
get an error (otherwise the message is simply dropped at once).

Thank you for clearing that up.

Once the pipe is there, you can send messages. The standard pattern
for a bound socket is to wait for an incoming message, which
guarantees the pipe is there. But you can also use other strategies,
like pausing and retrying.

I am actually following the standard pattern for a bound socket but not in the previous example code. Attached are the broker and client split up into two separate programs that can be run independently and lets the bound broker socket receive first and the connected client socket send after. And yes, like you said, the send after connect succeeds if there is a pause in between.

Would it be possible to make the connect call block till the pipe is set up? The failure would actually get handled by higher level reliability mechanisms, but I found the behaviour weird.

Thanks,
Anoop
import zmq

broker_uri = "tcp://127.0.0.1:20000"

context = zmq.Context()

broker = context.socket(zmq.ROUTER)
broker.setsockopt(zmq.IDENTITY, "BROKER")
broker.setsockopt(zmq.ROUTER_BEHAVIOR, 1)
broker.bind(broker_uri)

parts = broker.recv_multipart()
print(parts)
import zmq
import time

broker_uri = "tcp://127.0.0.1:20000"

context = zmq.Context()

client = context.socket(zmq.ROUTER)
client.setsockopt(zmq.IDENTITY, "CLIENT")
client.setsockopt(zmq.ROUTER_BEHAVIOR, 1)
client.connect(broker_uri)

time.sleep(1)

try:
    client.send_multipart(["BROKER", "Hello"])
except zmq.error.ZMQError, e:
    print("Error: %s" % e)
else:
    print("Success!")
_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev

Reply via email to