diff options
author | Micah Anderson <micah@riseup.net> | 2014-08-11 16:33:29 -0400 |
---|---|---|
committer | Micah Anderson <micah@riseup.net> | 2014-08-11 16:33:29 -0400 |
commit | cce638a8adf4e045ca5505afea4bda57753c31dd (patch) | |
tree | b5e139d3359ac5b8c7b1afa8acbb1b5b6051c626 /examples/gevent |
initial import of debian package
Diffstat (limited to 'examples/gevent')
-rw-r--r-- | examples/gevent/poll.py | 42 | ||||
-rw-r--r-- | examples/gevent/reqrep.py | 47 | ||||
-rw-r--r-- | examples/gevent/simple.py | 37 |
3 files changed, 126 insertions, 0 deletions
diff --git a/examples/gevent/poll.py b/examples/gevent/poll.py new file mode 100644 index 0000000..1daf80a --- /dev/null +++ b/examples/gevent/poll.py @@ -0,0 +1,42 @@ +import gevent +from zmq import green as zmq + +# Connect to both receiving sockets and send 10 messages +def sender(): + + sender = context.socket(zmq.PUSH) + sender.connect('inproc://polltest1') + sender.connect('inproc://polltest2') + + for i in xrange(10): + sender.send('test %d' % i) + gevent.sleep(1) + + +# create zmq context, and bind to pull sockets +context = zmq.Context() +receiver1 = context.socket(zmq.PULL) +receiver1.bind('inproc://polltest1') +receiver2 = context.socket(zmq.PULL) +receiver2.bind('inproc://polltest2') + +gevent.spawn(sender) + +# Create poller and register both reciever sockets +poller = zmq.Poller() +poller.register(receiver1, zmq.POLLIN) +poller.register(receiver2, zmq.POLLIN) + +# Read 10 messages from both reciever sockets +msgcnt = 0 +while msgcnt < 10: + socks = dict(poller.poll()) + if receiver1 in socks and socks[receiver1] == zmq.POLLIN: + print "Message from receiver1: %s" % receiver1.recv() + msgcnt += 1 + + if receiver2 in socks and socks[receiver2] == zmq.POLLIN: + print "Message from receiver2: %s" % receiver2.recv() + msgcnt += 1 + +print "%d messages received" % msgcnt 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() diff --git a/examples/gevent/simple.py b/examples/gevent/simple.py new file mode 100644 index 0000000..ae065b3 --- /dev/null +++ b/examples/gevent/simple.py @@ -0,0 +1,37 @@ +from gevent import spawn, spawn_later +import zmq.green as zmq + +# server +print zmq.Context +ctx = zmq.Context() +sock = ctx.socket(zmq.PUSH) +sock.bind('ipc:///tmp/zmqtest') + +spawn(sock.send_pyobj, ('this', 'is', 'a', 'python', 'tuple')) +spawn_later(1, sock.send_pyobj, {'hi': 1234}) +spawn_later(2, sock.send_pyobj, ({'this': ['is a more complicated object', ':)']}, 42, 42, 42)) +spawn_later(3, sock.send_pyobj, 'foobar') +spawn_later(4, sock.send_pyobj, 'quit') + + +# client +ctx = zmq.Context() # create a new context to kick the wheels +sock = ctx.socket(zmq.PULL) +sock.connect('ipc:///tmp/zmqtest') + +def get_objs(sock): + while True: + o = sock.recv_pyobj() + print 'received python object:', o + if o == 'quit': + print 'exiting.' + break + +def print_every(s, t=None): + print s + if t: + spawn_later(t, print_every, s, t) + +print_every('printing every half second', 0.5) +spawn(get_objs, sock).join() + |