Tornado: difference between initialize() and prepare()

Viewed 497

I am using Tornado as web server, and I've noticed from the official guide two functions, initialize and prepare, that are called before requests handling (POST, GET etc...). But I cannot understand the difference between the twos: it seems they can be used for same things. Can you explain me if the functions have different use cases?

1 Answers

If in doubt, use prepare(). prepare() is the appropriate place to do most things, because it can do anything that the regular handler methods can do (including calling self.write() or self.render(), or raising tornado.web.HTTPError).

Only use initialize() to process the arguments received from the URLSpec (nearly always by saving them to the instance variables). initialize may not call methods like self.write, and exceptions it raises may not be handled as cleanly, so keep it simple.

Related