Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A small collection of useful utilities for the Tornado Webserver
Python
Tree: 47e5b38d03

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
tests
tornado_addons
.gitignore
README.md
__init__.py
setup.py

README.md

tornado_addons

This is a collection of addons and accompanying unit tests for the excellent Tornado web server

These addons are licensed the same as Tornado under the Apache Licence, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html).

Installation

Just place this directory in your python path.

Testing

Every addon will have a set of accompanying unit tests. We prefer to run them via Nose.

cd tornado_addons/
nosetests

Usage

The best source of information is the comments in routes.py or async_yield.py. They're fairly well commented and give a better understanding of the libs.

routes

Here's a simple example.

import tornado.web
from tornado_addons.route import route

@route('/blah')
class SomeHandler(tornado.web.RequestHandler):
    pass

t = tornado.web.Application(route.get_routes(), {'some app': 'settings'}

Async yields

Here's an example with normal callbacks.

from tornado.httpclient import AsyncHTTPClient

@tornado.web.asynchronous
class SomeHandler(tornado.web.RequestHandler):
    def get(self):
        self.somedata = 'xxx'
        AsyncHTTPClient.fetch( 'http://over/there',
            callback=self.my_async_http_cb )

    def my_async_http_cb(self, fetch_data):
        # do stuff with fetchdata here
        self.write(self.somedata)

Or, you can wrap your methods with async_yield...

from tornado_addons.async_yield import async_yield, AsyncYieldMixin

class SomeHandler(tornado.web.RequestHandler, AsyncYieldMixin):
    @async_yield
    def get(self):
        somedata = 'xxx'
        fetchdata = yield AsyncHTTPClient.fetch( 'http://over/there',
                              callback=self.yield_cb )
        # do stuff with fetchdata here
        self.write(somedata)

The @async_yield wrapper doesn't work for every method with callbacks but it does cleanup your RequestHandlers quite nicely and really streamlines workflow.

You begin to see the real power of this when you start to have handlers that make multiple async calls.

Something went wrong with that request. Please try again.