Lore
Lore

Reputation: 1928

Tornado: difference between initialize() and prepare()

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?

Upvotes: 5

Views: 1313

Answers (1)

Ben Darnell
Ben Darnell

Reputation: 22134

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.

Upvotes: 4

Related Questions