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