接口测试 py3.5 aiohttp 百万请求?

测试小书童 · 2016年05月12日 · 最后由 jacexh 回复于 2016年05月12日 · 1411 次阅读

说明

例子一

import asyncio
from aiohttp import ClientSession
#  你使用async以及await关键字将函数异步化
async def fetch(url):
    async with ClientSession() as session:
        async with session.get(url) as response:
            return await response.read()
async def run(loop,  r):
    url = "http://gc.ditu.aliyun.com/geocoding?a=苏州市"
    tasks = []
    for i in range(r):
        task = asyncio.ensure_future(fetch(url.format(i)))
        tasks.append(task)

    responses = await asyncio.gather(*tasks)
    # 注意asyncio.gather()的用法,它搜集所有的Future对象,然后等待他们返回。
    # print(json.loads(responses[0].decode()))
    print(len(responses))

loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run(loop, 800))
loop.run_until_complete(future)

  • future = asyncio.ensure_future(run(loop, 800)) 这里 构造 1000 个请求时,就报 too many open files loop is not close 作者也遇到此问题,说是本机的 socket 端口用光了?很是怀疑

例子二 asyncio.Semaphore 解决报错问题

import asyncio
from aiohttp import ClientSession

async def fetch(url):
    async with ClientSession() as session:
        async with session.get(url) as response:
            return await response.read()


async def bound_fetch(sem, url):
    async with sem:
        await fetch(url)

async def run(loop,  r):
    url = "http://gc.ditu.aliyun.com/geocoding?a=苏州市"
    tasks = []
    # create instance of Semaphore
    sem = asyncio.Semaphore(100)
    for i in range(r):
        # pass Semaphore to every GET request
        task = asyncio.ensure_future(bound_fetch(sem, url.format(i)))
        tasks.append(task)

    responses = await asyncio.gather(*tasks)
    print(responses)
number = 100000
loop = asyncio.get_event_loop()

future = asyncio.ensure_future(run(loop, number))
loop.run_until_complete(future)
  • 然而我用 asyncio.Semaphore 时,发现请求不成功,已经发了邮件给作者,没有回我。
  • 嗯,我坑了,但是觉得还是很值得学习。。勿喷。还是觉得 tornaod 简单。。。
  • 下次研究下 tusng
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 1 条回复 时间 点赞

我用 aiohttp 开发过一个性能测试工具
但说实话,python3 async 性能不如 tornaod,更不如 gevent
简单玩玩还可以

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册