【发布时间】:2017-02-02 19:25:29
【问题描述】:
我正在尝试使用aiohttp 创建一个网络流量模拟器。以下代码示例异步发出 10k 个请求。我想知道其中有多少是同时发生的,所以我可以说这模拟了 10k 个同时请求网站的用户。
如何确定并发网络请求的数量或如何确定 aiohttp 每秒发出多少请求?有没有办法实时调试/分析并发请求的数量?
有没有更好的方法来使用任何其他编程语言对网络流量模拟器进行建模?
import asyncio
import aiohttp
async def fetch(session, url):
with aiohttp.Timeout(10, loop=session.loop):
async with session.get(url) as response:
return await response.text()
async def run(r):
url = "http://localhost:3000/"
tasks = []
# Create client session that will ensure we dont open new connection
# per each request.
async with aiohttp.ClientSession() as session:
for i in range(r):
html = await fetch(session, url)
print(html)
# make 10k requests per second ?? (not confident this is true)
number = 10000
loop = asyncio.get_event_loop()
loop.run_until_complete(run(number))
【问题讨论】:
标签: python python-3.x async-await python-asyncio aiohttp