完整代码如下:
import asyncio
import time
async def downloader(url):
# 模拟下载
time.sleep(3)
res = "download from{}".format(url)
return res
async def handle(url_list):
res = ''
for url in url_list:
html = await downloader(url)
res += html
return res
if __name__ == "__main__":
urls = ["http://www.baidu.com", "http://www.sina.com"]
coro = handle(urls)
start_time = time.time()
try:
coro.send(None)
except StopIteration as e:
print(e.value)
print("耗时:", time.time()-start_time)