from tornado import gen
from tornado.httpclient import AsyncHTTPClient
@gen.coroutine
def coroutine_visit_list():
http_client = AsyncHTTPClient()
list_response = yield [
http_client.fetch('www.baidu.com'),
http_client.fetch('www.sina.com'),
http_client.fetch('www.163.com')
]
for response in list_response:
print(response)
@gen.coroutine
def coroutine_visit_dict():
http_client = AsyncHTTPClient()
dict_response = yield {
'baidu':http_client.fetch('www.baidu.com'),
'sina':http_client.fetch('www.sina.com'),
'163':http_client.fetch('www.163.com')}
print(dict_response['sina'])
tornado允许一个yield等待多个异步调用,只需用列表或者字典方式传递即可,传递方式是什么,则tornado返回异步调用的结果也是相同的方式!