Unexpected Performance Results: httpx Async Slower than aiohttp and httpx Sync #3457
-
|
Asynchronous requests with httpx are much slower than those with aiohttp, and surprisingly, even slower than httpx's synchronous requests. Test Results: Test Script: import asyncio
import httpx
import aiohttp
import time
async def ah():
async with httpx.AsyncClient(timeout=3600) as client:
response = await client.get('https://connect.rom.miui.com/generate_204')
print(response.status_code, response.text)
async def aio():
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=3600)) as client:
async with client.get('https://connect.rom.miui.com/generate_204') as resp:
data = await resp.text()
print(resp.status, data)
def ah_sync():
with httpx.Client(timeout=3600) as client:
response = client.get('https://connect.rom.miui.com/generate_204')
print(response.status_code, response.text)
async def mutli_ah():
tasks = [ah() for _ in range(100)]
await asyncio.gather(*tasks)
async def mutli_aio():
tasks = [aio() for _ in range(100)]
await asyncio.gather(*tasks)
async def mutli_ah_2():
tasks = [asyncio.to_thread(ah_sync) for _ in range(100)]
await asyncio.gather(*tasks)
t1 = time.perf_counter()
asyncio.run(mutli_ah())
print('httpx AsyncClient', time.perf_counter() - t1, 's')
# ------------------------------
# asyncio.run(mutli_aio())
# print('aiohttp AsyncClient', time.perf_counter() - t1, 's')
# ------------------------------
# asyncio.run(mutli_ah_2())
# print('httpx Client with asyncio.to_thread', time.perf_counter() - t1) |
Beta Was this translation helpful? Give feedback.
Answered by
brianler
Dec 25, 2025
Replies: 1 comment
-
|
When reusing the client side, the gap between the two is small. Test Results: Test Script: import asyncio
import aiohttp
import httpx
async def ah(client):
await client.get("https://www.google.com/generate_204")
async def aio(client):
async with client.get("https://www.google.com/generate_204") as resp:
await resp.text()
async def mutli_ah():
async with httpx.AsyncClient(timeout=3600) as client:
stime = asyncio.get_event_loop().time()
tasks = [ah(client) for _ in range(100)]
await asyncio.gather(*tasks)
print("httpx AsyncClient", asyncio.get_event_loop().time() - stime, "s")
async def mutli_aio():
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=3600)) as client:
stime = asyncio.get_event_loop().time()
tasks = [aio(client) for _ in range(100)]
await asyncio.gather(*tasks)
print("aiohttp AsyncClient", asyncio.get_event_loop().time() - stime, "s")
async def main():
await mutli_aio()
await mutli_ah()
if __name__ == "__main__":
asyncio.run(main())🚀 When http2 is enabled and there are a large number of connections, httpx performs better than aiohttp. async with httpx.AsyncClient(timeout=3600, http2=True) as client:
... |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
brianler
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When reusing the client side, the gap between the two is small.
Test Results:
Test Script: