0
我的處理程序中有一部分異步代碼,我需要將此部分作爲外部方法並從不同的處理程序運行,但每個方法都有異步代碼。你可以幫我嗎?異步方法中的外部異步方法
簡單的例子:
#!/usr/bin/python
import asyncmongo
import tornado.web
class Handler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.db = asyncmongo.Client(pool_id='mypool', host='localhost',
port=27107, dbname='mydb')
self.db.first_names.find_one({'user_id': 1 },
callback=self._on_response)
def _on_response(self, response, error):
first_name = response['first_name']
data = {
'first_name': first_name
}
# use processor
first_name_html = self.generate_html("firstname.html").generate(**data)
last_name_html = foo()
self.write(first_name_html + last_name_html)
self.finish()
# this part of code is wrong!
# I have question about it
@tornado.web.asynchronous
def foo(self):
self.db.last_names.find_one({'user_id': 1 },
callback=self._on_response_two)
def _on_response_two(self, response, error):
last_name = response['last_name']
data = {
'last_name': last_name
}
# use processor
last_name_html = self.generate_html("lastname.html").generate(**data)
return last_name_html
這似乎是一個用戶應該有一個first_name和last_name是否有一個原因,他們被分成2個不同的集合? – dm03514