Russ Weeks
Russ Weeks

Reputation: 373

How to build a thread-safe Request Handler with Python Tornado

Does Tornado make any guarantees about how many requests are concurrently handled by a single RequestHandler? I'm thinking about managing session data like this:

class MyHandler(tornado.web.RequestHandler):
  def prepare(self):
    self.session = load_session_from_memcached

  def get(self):
    # work with self.session

  def on_finish(self):
    save_session_to_memcached(self.session)

But this is only going to work if self.session in get() is the same as self.session in prepare(). And that is only going to be true if a single RequestHandler processes at most one request concurrently. Is this how Tornado works?

Upvotes: 2

Views: 1285

Answers (1)

jjwchoy
jjwchoy

Reputation: 1908

RequestHandlers are instantiated per request. A single request handler instance will serve one and only one request in its lifetime


Edit:

Here's the link to the part of the documentation which confirms my statement above: http://www.tornadoweb.org/documentation/overview.html#overriding-requesthandler-methods

See Item 1: A new RequestHandler object is created on each request

Upvotes: 10

Related Questions