pythonasynchronoustornadononblockingrequesthandler

where to cache stuff for RequestHandler?


I intend to use tornado to write a handler that implement an autocomplete service, demonstrated as below:

class AutoCompleteHandler(tornado.web.RequestHandler):

    def initialize(self, indexbuilder):
        self.indexbuilder = indexbuilder
        self.index = indexbuilder.build_merged_index()

    def get(self):

        query = self.get_argument('q')

        result = self.index[query]

        self.set_header("Content-Type", 'application/json;')
        self.set_header('charset', "utf-8")
        self.write(json.dumps(result))

The facts and requirements

The problem

In short, how can I do this right?

Where should I cache the index, but still having the non-blocking feature of tornado? (I guess) I could put indexbuilder under the same module as the AutoCompleteHandler and build the index to a global variable, and spawn a separate thread to do the refreshing task, but that doesn't look right to me and I think the work could be done using tornado and make the structure much more elegant.


Solution

  • A global variable sounds like the best/simplest solution to me. You can also attach it to the Application object (which is accessible as self.application within the RequestHandler if you'd prefer to avoid the global. Or you could cache it on the indexbuilder itself, or pass some cache object in the initialize dict.

    In any case you probably want to do the refresh in a separate thread to avoid blocking the IOLoop while it is running.