gpt4 book ai didi

python - Tornado ,在回调函数中访问附加数据?

转载 作者:太空宇宙 更新时间:2023-11-03 19:06:24 26 4
gpt4 key购买 nike

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

我找到了this帖子使用 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

最佳答案

关于python - Tornado ,在回调函数中访问附加数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14605915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com