1

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
4

1 に答える 1

1

tornado.genを使用すると、問題を完全に回避できます

http://www.tornadoweb.org/documentation/gen.html?highlight=tornado.gen#tornado.gen

于 2013-01-30T15:02:59.950 に答える