[英]Tornado, Accessing additional data in callback function?
我刚刚使用Tornado和asyncmongo启动了一个项目。
我有一个带有异步方法的处理程序。 在内部,我正在向mongo查询一些单词:
@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
在我的回调方法中,我需要使用原始单词对mongo结果进行准确排序。
我发现这篇文章使用functools.partial
通过允许我将其他参数传递给回调方法来完成此任务
我想知道是否会对在get
方法中设置实例属性并在_on_response
访问它产生任何不利影响? 谢谢
@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
使用tornado.gen,您完全可以避免问题
http://www.tornadoweb.org/documentation/gen.html?highlight=tornado.gen#tornado.gen
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.