def Server(context):
context = context or zmq.Context().instance()
# Socket facing clients
frontend = context.socket(zmq.ROUTER)
frontend.bind("tcp://*:5559")
# Socket facing services
backend = context.socket(zmq.ROUTER)
backend.bind("tcp://*:5560")
print "zmq server running on localhost:5559/5560"
poll_workers = zmq.Poller()
poll_workers.register(backend, zmq.POLLIN)
poll_both = zmq.Poller()
poll_both.register(backend, zmq.POLLIN)
poll_both.register(frontend, zmq.POLLIN)
clients = [[]]
while True:
if clients:
sockets = dict(poll_both.poll())
else:
sockets = dict(poll_workers.poll())
if sockets.get(frontend) == ZMQ.POLLIN:
clientRequest = frontend.recv_multipart()
clients.append(clientRequest[0]) #push client into queue
if sockets.get(backend) == ZMQ.POLLIN:
#workers want data
msg = backend.recv_multipart()
workerIdentity = msg[0]
clientIdentity,request = clients.pop(0)
workRequest = [workerIdentity, '',request]
backend.send_multipart(workRequest)
yelpResponse = backend.recv_multipart()[2]
#fulfill frontend request
frontendResponse = [clientIdentity,'',yelpResponse]
frontend.send_multipart(frontendResponse)
评论列表
文章目录