Inner @ gen.coroutine

时间:2017-01-12 20:45:09

标签: python tornado

我想运行异步方法,该方法运行其他异步方法,使用@ tornado.gen.coroutine装饰器运行其他异步方法。 这样的事情:

@gen.coroutine
def download1():
    http_client = AsyncHTTPClient()
    res = yield http_client.fetch("http://example.com")
    do_something_with_response1(res)
    raise tornado.gen.Return(res)

@gen.coroutine
def download2():
    http_client = AsyncHTTPClient()
    res = yield http_client.fetch("http://example2.com")
    do_something_with_response2(res)
    raise tornado.gen.Return(res)

@gen.coroutine
def big_download():
    res1 = yield download1()
    res2 = yield download2()
    res = do_something_with_responses(res1, res2)
    raise tornado.gen.Return(res)

class HelloHandler(RequestHandler):
    @gen.coroutine
    def get(self):
        res = yield big_download()
        self.write(res)

我可以这样做吗?怎么样?

0 个答案:

没有答案
相关问题