import aiohttp
import asyncio
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
async def main():
async with aiohttp.ClientSession() as session:
urls = ["http://example.com" for _ in range(5000)] # 假设有5000个请求
tasks = []
for url in urls:
task = asyncio.ensure_future(fetch(session, url)) # 创建任务
tasks.append(task)
responses = await asyncio.gather(*tasks) # 并发执行任务
# 处理响应...
# 运行异步主函数
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
python并发网络请求--aiohttp
最新推荐文章于 2024-06-27 15:03:57 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)