Permalink
Browse files

Merge pull request #8 from brimcfadden/master

Logging configuration & tutorial 6
  • Loading branch information...
2 parents 8c8857e + 59e81bf commit da4889b97f875688825e7f29933a96cfb44533b3 @paolo-losi committed Jul 15, 2011
@@ -1,4 +1,6 @@
#! /usr/bin/env python
+
+import logging
from tornado.ioloop import IOLoop
from stormed import Connection, Message
@@ -10,6 +12,7 @@ def on_connect():
def callback(msg):
print " [x] Received %r" % msg.body
+logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
@@ -1,5 +1,6 @@
#! /usr/bin/env python
+import logging
from tornado.ioloop import IOLoop
from stormed import Connection, Message
@@ -15,6 +16,7 @@ def done():
print " [x] Sent 'Hello World!'"
io_loop.stop()
+logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
@@ -1,5 +1,6 @@
#! /usr/bin/env python
+import logging
import sys
from tornado.ioloop import IOLoop
from stormed import Connection, Message
@@ -17,6 +18,7 @@ def done():
print " [x] Sent %r" % msg.body
io_loop.stop()
+logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
@@ -1,5 +1,6 @@
#! /usr/bin/env python
+import logging
import time
from tornado.ioloop import IOLoop
from stormed import Connection, Message
@@ -19,6 +20,7 @@ def done(msg):
print " [x] Done"
msg.ack()
+logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
@@ -1,5 +1,6 @@
#! /usr/bin/env python
+import logging
import sys
from tornado.ioloop import IOLoop
from stormed import Connection, Message
@@ -17,6 +18,7 @@ def done():
print " [x] Sent %r" % msg.body
io_loop.stop()
+logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
@@ -1,4 +1,6 @@
#! /usr/bin/env python
+
+import logging
from tornado.ioloop import IOLoop
from stormed import Connection, Message
@@ -17,6 +19,7 @@ def with_temp_queue(qinfo):
def callback(msg):
print " [x] %r" % msg.body
+logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
@@ -1,5 +1,6 @@
#! /usr/bin/env python
+import logging
import sys
from tornado.ioloop import IOLoop
from stormed import Connection, Message
@@ -18,6 +19,7 @@ def done():
print " [x] Sent %r:%r" % (severity, msg.body)
io_loop.stop()
+logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
@@ -1,4 +1,6 @@
#! /usr/bin/env python
+
+import logging
import sys
from tornado.ioloop import IOLoop
from stormed import Connection, Message
@@ -26,6 +28,7 @@ def with_temp_queue(qinfo):
def callback(msg):
print " [x] %r:%r" % (msg.rx_data.routing_key, msg.body)
+logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
@@ -1,5 +1,6 @@
#! /usr/bin/env python
+import logging
import sys
from tornado.ioloop import IOLoop
from stormed import Connection, Message
@@ -18,6 +19,7 @@ def done():
print " [x] Sent %r:%r" % (routing_key, msg.body)
io_loop.stop()
+logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
@@ -1,4 +1,6 @@
#! /usr/bin/env python
+
+import logging
import sys
from tornado.ioloop import IOLoop
from stormed import Connection, Message
@@ -26,6 +28,7 @@ def with_temp_queue(qinfo):
def callback(msg):
print " [x] %r:%r" % (msg.rx_data.routing_key, msg.body)
+logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+
+import logging
+import sys
+import uuid
+from tornado.ioloop import IOLoop
+from stormed import Connection, Message
+
+class FibonacciRpcClient(object):
+ def __init__(self, n):
+ self.conn = Connection(host='localhost')
+ self.conn.connect(self.on_connect)
+ self.n = n
+
+ def on_connect(self):
+ self.ch = self.conn.channel()
+ self.ch.queue_declare(exclusive=True, callback=self.on_queue_declare)
+
+ def on_queue_declare(self, q_info):
+ callback_queue = q_info.queue
+ self.ch.consume(callback_queue, self.on_response)
+ self.corr_id = str(uuid.uuid4())
+ msg = Message(str(self.n), delivery_mode=2, reply_to=callback_queue,
+ correlation_id=self.corr_id)
+ self.ch.publish(msg, exchange='', routing_key='rpc_queue')
+
+ def on_response(self, msg):
+ if self.corr_id == msg.correlation_id:
+ print " [x] Received %r" % msg.body
+ self.conn.close(callback=IOLoop.instance().stop)
+ print 'Closing connection.'
+
+logging.basicConfig()
+try:
+ n = int(sys.argv[1])
+except:
+ n = 30
+io_loop = IOLoop.instance()
+fibonacci_rpc = FibonacciRpcClient(n)
+print ' [x] Requesting fib(%s)' % n
+try:
+ io_loop.start()
+except:
+ io_loop.stop()
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+
+import logging
+from tornado.ioloop import IOLoop
+from stormed import Connection, Message
+
+def fib(n):
+ if n == 0:
+ return 0
+ elif n == 1:
+ return 1
+ else:
+ return fib(n-1) + fib(n-2)
+
+def on_connect():
+ global ch
+ ch = conn.channel()
+ ch.queue_declare(queue='rpc_queue', durable=True)
+ ch.qos(prefetch_count=1)
+ ch.consume('rpc_queue', on_request)
+
+def on_request(msg):
+ n = int(msg.body)
+ print " [.] fib(%s)" % n
+ response = str(fib(n))
+ response_msg = Message(response, delivery_mode=2,
+ correlation_id=msg.correlation_id)
+ ch.publish(response_msg, exchange='', routing_key=msg.reply_to)
+ msg.ack()
+
+logging.basicConfig()
+ch = None
+conn = Connection(host='localhost')
+conn.connect(on_connect)
+io_loop = IOLoop.instance()
+print ' [*] Waiting for messages. To exit press CTRL+C'
+try:
+ io_loop.start()
+except KeyboardInterrupt:
+ conn.close(io_loop.stop)

0 comments on commit da4889b

Please sign in to comment.