public
Last active

A barebones example of Tornado's asynchronous functionality.

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
 
class BaseHandler(tornado.web.RequestHandler):
pass
class HandlerMixin(object):
listeners = []
def add_listener(self, callback):
handler = HandlerMixin
handler.listeners.append(callback)
print 'Total listeners: %i' % len(handler.listeners)
def send_message(self, message):
handler = HandlerMixin
print 'Sending messages to %i listeners' % len(handler.listeners)
for listener in handler.listeners:
listener(message)
handler.listeners = []
class MainHandler(BaseHandler, HandlerMixin):
@tornado.web.asynchronous
def get(self):
self.add_listener(self.async_callback(self._callback))
def _callback(self, message):
if self.request.connection.stream.closed():
return
self.finish(message)
class MessageHandler(BaseHandler, HandlerMixin):
def get(self):
self.write('''
<form method="post">
<input type="text" name="message" />
<input type="submit" />
</form>
''')
def post(self):
message = self.get_argument('message', None)
self.send_message(message)
self.redirect(self.request.uri)
 
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r'/', MainHandler),
(r'/post/?', MessageHandler),
]
settings = dict()
tornado.web.Application.__init__(self, handlers, **settings)
 
def main():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
 
if __name__ == '__main__':
main()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.