Created
December 9, 2010 11:46
-
-
Save tonimichel/734639 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zmq | |
import sys | |
import time | |
# Call start the client like this: python client.py addr1 addr2 | |
# ------------------------------------------------------------------------------ | |
def send(socket, msg, depth=0, max_depth=4, timeout=0.9): | |
'''Nonblocking send noticing server deaths. Sends a message and waits for | |
``timeout`` seconds for a reply, in case no reply is received, the message is resent | |
''' | |
socket.send(msg) | |
t = time.time() | |
while True: | |
try: | |
reply = socket.recv(zmq.NOBLOCK) | |
except zmq.ZMQError, e: | |
if e.errno == zmq.EAGAIN: | |
# no message was ready | |
if depth == max_depth: | |
raise Exception("max depth reached") | |
if time.time() >= t + timeout: | |
# no message was received for ``timeout`` seconds | |
return send(socket, msg, depth+1) | |
else: | |
raise # real error | |
else: | |
return reply | |
# ------------------------------------------------------------------------------ | |
servers = sys.argv[1:] | |
context = zmq.Context() | |
socket = context.socket(zmq.XREQ) | |
for addr in servers: | |
socket.connect(addr) | |
for i in range(0, 10): | |
reply = send(socket, "msg " + str(i)) | |
print reply |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zmq | |
import sys | |
# call this script like this: server.py addr | |
address = sys.argv[1] | |
context = zmq.Context() | |
socket = context.socket(zmq.XREP) | |
socket.bind(address) | |
while True: | |
msg = socket.recv_multipart() | |
socket.send_multipart(msg) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment