From da5a20f2c86eef9c46a1685a51e080c3911e95ff Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 8 Oct 2017 18:09:57 +0200 Subject: zmq remote control example: use timeout on receive --- doc/zmq-ctrl/zmq_remote.py | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'doc') diff --git a/doc/zmq-ctrl/zmq_remote.py b/doc/zmq-ctrl/zmq_remote.py index 3a54b20..9e49b82 100755 --- a/doc/zmq-ctrl/zmq_remote.py +++ b/doc/zmq-ctrl/zmq_remote.py @@ -7,6 +7,9 @@ context = zmq.Context() sock = context.socket(zmq.REQ) +poller = zmq.Poller() +poller.register(sock, zmq.POLLIN) + if len(sys.argv) < 2: print("Usage: program url cmd [args...]") sys.exit(1) @@ -19,24 +22,33 @@ message_parts = sys.argv[2:] print("ping") sock.send(b"ping") -data = sock.recv_multipart() -print("Received: {}".format(len(data))) -for i,part in enumerate(data): - print(" {}".format(part)) -for i, part in enumerate(message_parts): - if i == len(message_parts) - 1: - f = 0 - else: - f = zmq.SNDMORE +socks = dict(poller.poll(1000)) +if socks: + if socks.get(sock) == zmq.POLLIN: + + data = sock.recv_multipart() + print("Received: {}".format(len(data))) + for i,part in enumerate(data): + print(" {}".format(part)) + + for i, part in enumerate(message_parts): + if i == len(message_parts) - 1: + f = 0 + else: + f = zmq.SNDMORE + + print("Send {}({}): '{}'".format(i, f, part)) - print("Send {}({}): '{}'".format(i, f, part)) + sock.send(part.encode(), flags=f) - sock.send(part.encode(), flags=f) + data = sock.recv_multipart() -data = sock.recv_multipart() + print("Received: {}".format(len(data))) + for i,part in enumerate(data): + print(" RX {}: {}".format(i, part)) -print("Received: {}".format(len(data))) -for i,part in enumerate(data): - print(" RX {}: {}".format(i, part)) +else: + print("ZMQ error: timeout") + context.destroy(linger=5) -- cgit v1.2.3