-
-
Save faith0811/34ae622abf2560820ab7 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
""" | |
Complex example which is a combination of the rr* examples from the zguide. | |
""" | |
from gevent.monkey import patch_all | |
patch_all() | |
from gevent import spawn | |
import zmq.green as zmq | |
# server | |
context = zmq.Context() | |
socket = context.socket(zmq.REP) | |
socket.connect("tcp://localhost:5560") | |
def serve(socket): | |
while True: | |
message = socket.recv(copy=False) | |
print "Received request: ", message | |
socket.send("World", copy=False) | |
server = spawn(serve, socket) | |
# client | |
context = zmq.Context() | |
socket = context.socket(zmq.REQ) | |
socket.connect("tcp://localhost:5559") | |
# Do 10 requests, waiting each time for a response | |
def client(): | |
for request in range(1,10): | |
socket.send("Hello", copy=False) | |
message = socket.recv(copy=False) | |
print "Received reply ", request, "[", message, "]" | |
# broker | |
frontend = context.socket(zmq.ROUTER) | |
backend = context.socket(zmq.DEALER); | |
frontend.bind("tcp://*:5559") | |
backend.bind("tcp://*:5560") | |
def proxy(socket_from, socket_to): | |
while True: | |
m = socket_from.recv_multipart() | |
socket_to.send_multipart(m) | |
a = spawn(proxy, frontend, backend) | |
b = spawn(proxy, backend, frontend) | |
spawn(client).join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment