Skip to content

Loading…

Logging configuration & tutorial 6 #8

Merged
merged 4 commits into from

2 participants

@brimcfadden

I'm issuing a new pull request to avoid stacking the new commits on top of the old one.

@paolo-losi

small typo: it should be quue='rpc_queue'

Whoops. This must have gotten copied from something else.

@paolo-losi
Owner

Thanks! I've tested it againt tornado 1.2 and everything works as expected. Which tornado version have you tested?
(my question is totally unrelated with your pull request but I haven't had the chance to test tornado 2.0 yet)

There a couple of small typos and a couple of pep8 warnings:

$ pep8 *
rpc_client.py:9:1: E302 expected 2 blank lines, found 1
rpc_client.py:14:1: W293 blank line contains whitespace
rpc_server.py:13:21: E225 missing whitespace around operator

Do you want to take care of those or me to do that?

@brimcfadden

I tested it against Tornado 1.2.1.

I have some other things to do today, so you can edit the files to fit your preferred style. I tried to adhere to your general style and PEP08, but it looks like I missed those few minor things. The blank lines with whitespace are within a class (and I used a class to make it more like the original Pika example), which is consistent with how I usually use whitespace on blank lines inside indented code blocks (I match the indentation until the end of the block, in this case the class). I actually didn't know that was part of PEP08, which I have read several times.

Anyway, if there's anything you don't like about these files, feel free to change them.

@paolo-losi
Owner

Brian,

thanks again. I'm merging it...

@paolo-losi paolo-losi merged commit da4889b into paolo-losi:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 15, 2011
  1. @brimcfadden
  2. @brimcfadden

    Tutorial 6 added.

    brimcfadden committed
  3. @brimcfadden

    Fixing queue name.

    brimcfadden committed
  4. @brimcfadden

    Edited typo.

    brimcfadden committed
View
3 examples/tutorial1/receive.py
@@ -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()
View
2 examples/tutorial1/send.py
@@ -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()
View
2 examples/tutorial2/new_task.py
@@ -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()
View
2 examples/tutorial2/worker.py
@@ -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()
View
2 examples/tutorial3/emit_log.py
@@ -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()
View
3 examples/tutorial3/receive_logs.py
@@ -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()
View
2 examples/tutorial4/emit_log_direct.py
@@ -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()
View
3 examples/tutorial4/receive_logs_direct.py
@@ -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()
View
2 examples/tutorial5/emit_log_topic.py
@@ -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()
View
3 examples/tutorial5/receive_logs_topic.py
@@ -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()
View
44 examples/tutorial6/rpc_client.py
@@ -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()
View
40 examples/tutorial6/rpc_server.py
@@ -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)
Something went wrong with that request. Please try again.