Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use cases of RequestHandler.initialize() in Tornado

Tags:

python

tornado

Is it correct to say that one should use initialize method to prepare resources that will be shared by all other methods (e.g. get, post, etc) of a RequestHandler subclass?

What are the other common use cases for using initialize in Tornado? It'd be great to have some examples!

like image 550
skyork Avatar asked Sep 19 '12 22:09

skyork


1 Answers

Why you don't like example in tornado code?

def initialize(self):
    """Hook for subclass initialization.

    A dictionary passed as the third argument of a url spec will be
    supplied as keyword arguments to initialize().

    Example::

        class ProfileHandler(RequestHandler):
            def initialize(self, database):
                self.database = database

            def get(self, username):
                ...

        app = Application([
            (r'/user/(.*)', ProfileHandler, dict(database=database)),
            ])
    """
    pass
like image 134
Nikolay Fominyh Avatar answered Nov 11 '22 16:11

Nikolay Fominyh