I have just started a project using Tornado, and asyncmongo.
I have a handler with an async method. Inside I am querying mongo for some words:
@tornado.web.asynchronous
def get(self):
word = self.get_argument('word', None)
if not word:
self.write('{}')
self.finish()
self.db.spanish_dict.find({'$or': [{'word': word}, {'stem': word}]},
callback=self._on_response)
def _on_response(self, response, error):
# need to sort response by relevancy
In my callback method I need the original word to sort the mongo results accurately.
I found this post which uses functools.partial
to accomplish this, by allowing me to pass additional parameters to the callback method
I was wondering if there are any adverse affects to setting an instance attribute in the get
method and accessing it in _on_response
? THank you
@tornado.web.asynchronous
def get(self):
word = self.get_argument('word', None)
if not word:
self.write('{}')
self.finish()
self.word = word
self.db.spanish_dict.find({'$or': [{'word': word}, {'stem': word}]},
callback=self._on_response)
def _on_response(self, response, error):
# need to sort response by relevancy
# will self.word always be accurate?
self.word
Use tornado.gen and you sidestep the problem completely
http://www.tornadoweb.org/documentation/gen.html?highlight=tornado.gen#tornado.gen
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.