繁体   English   中英

龙卷风,在回调函数中访问其他数据?

[英]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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM