Skip to content

Commit

Permalink
examples for tutorial 5
Browse files Browse the repository at this point in the history
  • Loading branch information
paolo-losi committed Jan 10, 2011
1 parent 5ab4923 commit 0e76637
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
24 changes: 24 additions & 0 deletions 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()
36 changes: 36 additions & 0 deletions 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)

0 comments on commit 0e76637

Please sign in to comment.