tornado内使用Future中数据用什么方法,tornadofuture,tonado官方文档(h


tonado官方文档(http://www.tornadoweb.org/en/stable/gen.html)
可以通过定义以下方法获取异步返回值:
@gen.coroutine
def fetch_json(url):
response = yield AsyncHTTPClient().fetch(url)
raise gen.Return(json_decode(response.body))

但是以下代码调用fetch_json方法,得到一个数据(tornado.concurrent.Future),在get方法里面怎么获取返回的response.body的值?

class TestHandler(BaseHtHandler):
@asynchronous
def get(self):
log.info("TestHandler.get")
self.set_header("Access-Control-Allow-Origin", "*")
response=self.fetch_json("http://www.baidu.com")
self.finish()

from tornado.web import RequestHandler, Application, asynchronousfrom tornado.ioloop import IOLoopfrom tornado.httpserver import HTTPServerfrom tornado import genfrom tornado.httpclient import AsyncHTTPClientclass IndexHandler(RequestHandler):    @asynchronous    @gen.coroutine    def get(self):        response = self.fetch_json('http://www.qq.com')        result = yield response        self.write(result)        self.finish()    @gen.coroutine    def fetch_json(self, url):        response = yield AsyncHTTPClient().fetch(url)        raise gen.Return(response.body)app = Application([('/', IndexHandler)], debug = True)http_server = HTTPServer(app)http_server.listen(1888)IOLoop.instance().start()

编橙之家文章,

评论关闭