summaryrefslogtreecommitdiff
path: root/examples/gevent/reqrep.py
diff options
context:
space:
mode:
authorMicah Anderson <micah@riseup.net>2014-08-11 16:33:29 -0400
committerMicah Anderson <micah@riseup.net>2014-08-11 16:33:29 -0400
commitcce638a8adf4e045ca5505afea4bda57753c31dd (patch)
treeb5e139d3359ac5b8c7b1afa8acbb1b5b6051c626 /examples/gevent/reqrep.py
initial import of debian package
Diffstat (limited to 'examples/gevent/reqrep.py')
-rw-r--r--examples/gevent/reqrep.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/gevent/reqrep.py b/examples/gevent/reqrep.py
new file mode 100644
index 0000000..2a4f307
--- /dev/null
+++ b/examples/gevent/reqrep.py
@@ -0,0 +1,47 @@
+"""
+Complex example which is a combination of the rr* examples from the zguide.
+"""
+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()
+ print "Received request: ", message
+ socket.send("World")
+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")
+ message = socket.recv()
+ 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()