Tornado,访问回调函数中的其他数据?

2024-06-16 12:35:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始了一个使用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结果进行排序。在

我找到了thispost,它使用functools.partial来完成这项工作,允许我向回调方法传递其他参数

我想知道在get方法中设置实例属性并在_on_response中访问它是否有任何不利影响?谢谢

^{pr2}$

Tags: 项目方法selfweb处理程序getonresponse