-
Notifications
You must be signed in to change notification settings - Fork 19
Decorators
RequestHandler decorators are functions that wrap your request handlers to provide easy ways to apply helpful functionality to your handlers. Tornado comes with some out of the box like for instance the @authenticated decorator. We've added few more.
Tornado is single threaded. This means that if you ever block on IO (ex, database call) your core will just sit idle until that call returns. We have added the @threaded decorator. When added to any controller action, the controller code will execute in a pooled thread. Control is given back to the main IO thread for template rendering. We feel this is a much easier solution than trying to do all database interaction asynchronously. Please note that using the @threaded decorator will not give any performance gains unless there are some blocking calls.
from whirlwind.core.multithreading import threaded
class MyHandler(RequestHandler):
@threaded
def get(self):
#Do some blocking IO calls here
pass
Often you may want to have more fine grained control over which users can look at a page. The role_required decorator allows you to lock down requests so their only visible to authenticated users who have the specified role associated with their user object.
from whirlwind.view.decorators import role_required
class MyHandler(RequestHandler):
@role_required('admin')
def get(self):
pass
Whirlwind allows you to define the url route associated with a RequestHandler in two ways. First is via a config file called routes.py and secondly via the @route decorator. The route decorator takes a single argument that defines the web accessible path. This can be either a string or a regex. If you use a regex any groups defined in the regex will automatically be passed into your functions as arguments.
from whirlwind.view.decorators import route
@route('/')
class MyHandler(RequestHandler):
def get(self):
pass
@route(r'/(welcome|index)')
class MyHandler2(RequestHandler):
def get(self,mode):
pass
This decorator is a simple wrapper around the MongoKit decorator @connection.register which is used to register a connection to MongoDB.
@connection.register