Search code examples
pythontornado

Use cases of RequestHandler.initialize() in 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!


Solution

  • 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