我的处理程序中有一些异步代码,我需要将此部分作为外部方法并从不同的处理程序运行,但每个方法都有异步代码。你能帮助我吗?
简单的例子:
#!/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