触发大量HTTP请求并终止而不等待请求

时间:2018-03-30 11:10:42

标签: python-3.x http python-asyncio

对于某些测试,我想发送大量(~10,000)HTTP GET请求。

我对这些回复不感兴趣,并且不希望我的应用程序等待它们,所以它可以尽快完成。出于这个原因,我尝试使用requests库非常糟糕:

import requests

def fire(urls, params):
    for url in urls:
        for param in params:
            requests.get(url, params=param)

如何在不等待或处理响应的情况下发送大量HTTP请求?

1 个答案:

答案 0 :(得分:2)

您可以使用aiohttp并行触发多个请求,然后asyncio.wait_for()

import asyncio
import aiohttp

async def one(session, url):
    # request the URL and read it until complete or canceled
    async with session.get(url) as resp:
        await resp.text()

async def fire(urls):
    loop = asyncio.get_event_loop()
    async with aiohttp.ClientSession() as session:
        tasks = []
        for url in urls:
            tasks.append(loop.create_task(one(session, url)))

        # give tasks a second to run in parallel and do their thing,
        # then cancel them
        try:
            await asyncio.wait_for(asyncio.gather(*tasks), 1)
        except asyncio.TimeoutError:
            pass

loop = asyncio.get_event_loop()
loop.run_until_complete(fire([urls...]))