import asyncio
import aiohttp
import time
url_lst_failed=[]
url_lst_successed=[]
async def get_info(url):
async with aiohttp.ClientSession() as session:
async with session.get(url,timeout=10) as resp:
if resp.status != 200:
url_lst_failed.append(url)
else:
url_lst_successed.append(url)
r = await resp.text()
start = time.time()
#创建一个循环
loop = asyncio.get_event_loop()
#创建一个任务盒子tasks,包含了3个需要完成的任务
tasks =[get_info('http://127.0.0.1:5000/asyn/') for i in range(500)]
#tasks接入loop中开始运行
loop.run_until_complete(asyncio.wait(tasks))
end = time.time()
print(end-start)
print(len(url_lst_successed))
来源:CSDN
作者:huanghong6956
链接:https://blog.csdn.net/huanghong6956/article/details/103496826