Python 内部@gen.coroutine
我想运行异步方法,它运行其他异步方法,通过使用@tornado.gen.coroutine decorator运行其他异步方法。 诸如此类:Python 内部@gen.coroutine,python,tornado,Python,Tornado,我想运行异步方法,它运行其他异步方法,通过使用@tornado.gen.coroutine decorator运行其他异步方法。 诸如此类: @gen.coroutine def download1(): http_client = AsyncHTTPClient() res = yield http_client.fetch("http://example.com") do_something_with_response1(res) raise tornado.
@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)
我能做这个吗?如何操作?您的代码看起来是正确的。试着运行它。你有问题吗?你的代码没问题。另外,
big_download
可以启动两个下载res1,res2=yield[download1(),download2()]