Created
August 31, 2011 18:40
-
-
Save brimcfadden/1184323 to your computer and use it in GitHub Desktop.
Use stormed-amqp with tornado.web.RequestHandlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# tornadoweb_stormed.py | |
import logging | |
import sys | |
import stormed | |
import tornado.ioloop | |
import tornado.web | |
__author__ = 'Brian McFadden' | |
__email__ = '[email protected]' | |
HTML_HEADER = '<html><head><title>Tornado/Stormed RPC</title></head><body>' | |
HTML_FOOTER = '</body></html>' | |
class Fib(tornado.web.RequestHandler): | |
"""This is a different version of the file implementing tutorial #6. | |
The original version is located at https://gist.github.com/1150229/ | |
This version always raises an HTTP 500 error which causes some unexplained | |
behavior. | |
""" | |
@tornado.web.asynchronous | |
def get(self, number=''): | |
if not number: | |
self.redirect('/30') # GET / --> GET /30 | |
self.number = number | |
mq_conn = self.application.settings.get('mq_conn') | |
self.mq_ch = mq_conn.channel() | |
self.mq_ch.queue_declare(exclusive=True, | |
callback=self._on_q_declare) | |
def _on_q_declare(self, qinfo): | |
self.mq_ch.consume(qinfo.queue, self._on_mq_response) | |
self.corr_id = corr_id = str(id(self)) | |
msg = stormed.Message(str(self.number), delivery_mode=2, | |
reply_to=qinfo.queue, correlation_id=corr_id) | |
self.mq_ch.publish(msg, exchange='', routing_key='rpc_queue') | |
def _on_mq_response(self, response_msg): | |
self.send_error(500) | |
def main(): | |
logging.basicConfig(level=logging.DEBUG) | |
mq_conn = stormed.Connection(host='localhost') | |
def on_connect(): | |
print 'Connected to AMQP broker.' | |
mq_conn.connect(on_connect) | |
application = tornado.web.Application( | |
[(r'/([0-9]*)', Fib)], | |
**{'mq_conn': mq_conn} | |
) | |
try: | |
port = int(sys.argv[1]) # $ python tornadoweb_stormed.py 80 | |
except: | |
port = 8080 | |
application.listen(port) | |
print "Tornado is serving on port {0}.".format(port) | |
tornado.ioloop.IOLoop.instance().start() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The update to this gist solves the problem as described in my last comment, but the redirect still causes problems.