Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

examples for tutorial 5

  • Loading branch information...
commit 0e76637a4de687950d3305220851503cf77fc0a4 1 parent 5ab4923
@paolo-losi authored
View
24 examples/tutorial5/emit_log_topic.py
@@ -0,0 +1,24 @@
+#! /usr/bin/env python
+
+import sys
+from tornado.ioloop import IOLoop
+from stormed import Connection, Message
+
+# delivery_mode=2 makes message persistent
+routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
+msg = Message(' '.join(sys.argv[2:]) or 'Hello World!')
+
+def on_connect():
+ ch = conn.channel()
+ ch.exchange_declare(exchange='topic_logs', type='topic')
+ ch.publish(msg, exchange='topic_logs', routing_key=routing_key)
+ conn.close(callback=done)
+
+def done():
+ print " [x] Sent %r:%r" % (routing_key, msg.body)
+ io_loop.stop()
+
+conn = Connection(host='localhost')
+conn.connect(on_connect)
+io_loop = IOLoop.instance()
+io_loop.start()
View
36 examples/tutorial5/receive_logs_topic.py
@@ -0,0 +1,36 @@
+#! /usr/bin/env python
+import sys
+from tornado.ioloop import IOLoop
+from stormed import Connection, Message
+
+binding_keys = sys.argv[1:]
+if not binding_keys:
+ print >> sys.stderr, "Usage: %s [binding_key] ..." % sys.argv[0]
+ sys.exit(1)
+
+ch = None
+
+def on_connect():
+ global ch
+ ch = conn.channel()
+ ch.exchange_declare(exchange='topic_logs', type='topic')
+ ch.queue_declare(exclusive=True, callback=with_temp_queue)
+
+def with_temp_queue(queue_name, message_count, consumer_count):
+ for binding_key in binding_keys:
+ ch.queue_bind(exchange='topic_logs',
+ queue=queue_name,
+ routing_key=binding_key)
+ ch.consume(queue_name, callback, no_ack=True)
+
+def callback(msg):
+ print " [x] %r:%r" % (msg.rx_data.routing_key, msg.body)
+
+conn = Connection(host='localhost')
+conn.connect(on_connect)
+io_loop = IOLoop.instance()
+print ' [*] Waiting for logs. To exit press CTRL+C'
+try:
+ io_loop.start()
+except KeyboardInterrupt:
+ conn.close(io_loop.stop)
Please sign in to comment.
Something went wrong with that request. Please try again.