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
wordtreebuilder.build_merged_index
is an extremely slow method and is planned to run every 24h to refresh the index list. - According to tornado documentation, A new RequestHandler object is created on each request. So having the
treebuilder
as an instance attribute won't work for me.
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.