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