对于某些测试,我想发送大量(~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请求?
答案 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...]))