~ubuntu-branches/ubuntu/wily/pyzmq/wily

« back to all changes in this revision

Viewing changes to examples/gevent/reqrep.py

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-02-24 19:23:15 UTC
  • mfrom: (1.2.1) (9 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: package-import@ubuntu.com-20130224192315-qhmwp3m3ymk8r60d
Tags: 2.2.0.1-1
* New upstream release
* relicense debian packaging to LGPL-3
* update watch file to use github directly
  thanks to Bart Martens for the file
* add autopkgtests
* drop obsolete DM-Upload-Allowed
* bump standard to 3.9.4, no changes required

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Complex example which is a combination of the rr* examples from the zguide.
 
3
"""
 
4
from gevent import spawn
 
5
import zmq.green as zmq
 
6
 
 
7
# server
 
8
context = zmq.Context()
 
9
socket = context.socket(zmq.REP)
 
10
socket.connect("tcp://localhost:5560")
 
11
 
 
12
def serve(socket):
 
13
    while True:
 
14
        message = socket.recv()
 
15
        print "Received request: ", message
 
16
        socket.send("World")
 
17
server = spawn(serve, socket)
 
18
 
 
19
 
 
20
# client
 
21
context = zmq.Context()
 
22
socket = context.socket(zmq.REQ)
 
23
socket.connect("tcp://localhost:5559")
 
24
 
 
25
#  Do 10 requests, waiting each time for a response
 
26
def client():
 
27
    for request in range(1,10):
 
28
        socket.send("Hello")
 
29
        message = socket.recv()
 
30
        print "Received reply ", request, "[", message, "]"
 
31
 
 
32
 
 
33
# broker
 
34
frontend = context.socket(zmq.XREP)
 
35
backend  = context.socket(zmq.XREQ);
 
36
frontend.bind("tcp://*:5559")
 
37
backend.bind("tcp://*:5560")
 
38
 
 
39
def proxy(socket_from, socket_to):
 
40
    while True:
 
41
        m = socket_from.recv_multipart()
 
42
        socket_to.send_multipart(m)
 
43
 
 
44
a = spawn(proxy, frontend, backend)
 
45
b = spawn(proxy, backend, frontend)
 
46
 
 
47
spawn(client).join()