import time
import asyncio
import aiohttp
stat = time.time()
urls = ['http://127.0.0.1:5000/bobo',
'http://127.0.0.1:5000/jay',
'http://127.0.0.1:5000/ja']
async def get_page(url):
async with aiohttp.ClientSession() as session:
async with await session.get(url) as responsse:
"""
text()返回以字符串形式的响应数据
read()返回二进制形式的响应数据
json()返回json对象
注意:响应数据操作前一定要使用await进行手动挂起
"""
page_text = await responsse.text()
print(page_text)
tasks = []
for url in urls:
c = get_page(url)
task = asyncio.ensure_future(c)
tasks.append(task)
loop = asyncio.get_event_loop()
#异步协程固定写法 创建事件循环
loop.run_until_complete(asyncio.wait(tasks))
end = time.time()
print('总耗时:',end-stat)