Hi Pieter,

I've added my details to the code, signed it and created the patch (attached, since it's quite long).
You can find the patch in Gist as well: git://gist.github.com/620621.git

Piero

On 11/10/10 15:15, Pieter Hintjens wrote:
Hi Piero,

Thanks for this. Could you read the section 'Examples' at
http://github.com/imatix/zguide and follow the instructions there?

-Pieter

On Mon, Oct 11, 2010 at 4:13 PM, Piero Cornice <[email protected]> wrote:
As I'm learning pyzmq, I've written the first "peering" example in Python as
an exercise... hope it's correct.

I've put the source code in a public Gist at
git://gist.github.com/620558.git.

Cheers,
Piero
http://github.com/piero



_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev


>From bba1ccba7b1dcd84121c4af218df64482da88b90 Mon Sep 17 00:00:00 2001
From: Piero Cornice <[email protected]>
Date: Mon, 11 Oct 2010 15:36:42 +0100
Subject: [PATCH] Added peering1 example in Python.

Signed-off-by: Piero Cornice <[email protected]>
---
 examples/Python/peering1.py |   94 ++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 84 insertions(+), 10 deletions(-)

diff --git a/examples/Python/peering1.py b/examples/Python/peering1.py
index 69a8816..f8aa0d9 100644
--- a/examples/Python/peering1.py
+++ b/examples/Python/peering1.py
@@ -1,13 +1,87 @@
-No-one has translated the peering1 example into Python yet.  Be the first to create
-peering1 in Python and get one free Internet!  If you're the author of the Python
-binding, this is a great way to get people to use 0MQ in Python.
+#
+#   Broker peering simulation (part 1) in Python
+#   Prototypes the state flow
+#
+#   Author : Piero Cornice
+#   Contact: root(at)pieroland(dot)net
+#
 
-To submit a new translation email it to [email protected].  Please:
+import zmq
+import time
+import random
 
-* Stick to identical functionality and naming used in examples so that readers
-  can easily compare languages.
-* You MUST place your name as author in the examples so readers can contact you.
-* You MUST state in the email that you license your code under the MIT/X11
-  license.
+def main(args):
+
+    myself = args[1]
+    print "Hello, I am", myself
+
+    context = zmq.Context()
+
+    # State Back-End
+    statebe = context.socket(zmq.PUB)
+
+    # State Front-End
+    statefe = context.socket(zmq.SUB)
+    statefe.setsockopt(zmq.SUBSCRIBE, '')
+
+    bind_address = "ipc://" + myself + "-state.ipc"
+    statebe.bind(bind_address)
+
+    for i in range(len(args) - 2):
+        endpoint = "ipc://" + args[i + 2] + "-state.ipc"
+        statefe.connect(endpoint)
+        time.sleep(1.0)
+
+    poller = zmq.Poller()
+    poller.register(statefe, zmq.POLLIN)
+
+    while True:
+
+########## Solution with poll() ##########
+        socks = dict(poller.poll(1000))
+
+        try:
+            # Handle incoming status message
+            if socks[statefe] == zmq.POLLIN:
+                msg = statefe.recv_multipart()
+                print 'Received:', msg
+
+        except KeyError:
+            # Send our address and a random value
+            # for worker availability
+            msg = []
+            msg.append(bind_address)
+            msg.append(str(random.randrange(1, 10)))
+            statebe.send_multipart(msg)
+##################################
+
+######### Solution with select() #########
+#        (pollin, pollout, pollerr) = zmq.select([statefe], [], [], 1)
+#
+#        if len(pollin) > 0 and pollin[0] == statefe:
+#            # Handle incoming status message
+#            msg = statefe.recv_multipart()
+#            print 'Received:', msg
+#
+#        else:
+#            # Send our address and a random value
+#            # for worker availability
+#            msg = []
+#            msg.append(bind_address)
+#            msg.append(str(random.randrange(1, 10)))
+#            statebe.send_multipart(msg)
+##################################
+
+    poller.unregister(statefe)
+    time.sleep(1.0)
+
+
+if __name__ == '__main__':
+    import sys
+
+    if len(sys.argv) < 2:
+        print "Usage: peering.py <myself> <peer_1> ... <peer_N>"
+        raise SystemExit
+
+    main(sys.argv)
 
-Subscribe to this list at http://lists.zeromq.org/mailman/listinfo/zeromq-dev.
-- 
1.7.2.3

_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev

Reply via email to