Back to articles
requests vs httpx vs aiohttp — I Benchmarked All 3 (Results Surprised Me)

requests vs httpx vs aiohttp — I Benchmarked All 3 (Results Surprised Me)

via Dev.to TutorialAlex Spinov

I test HTTP libraries for a living I maintain 77 web scrapers. Each one makes thousands of HTTP requests per run. The choice of HTTP library matters. So I benchmarked the three most popular Python HTTP libraries head-to-head. The contenders Library Async HTTP/2 Connection Pooling Stars requests No No Yes 52k httpx Yes Yes Yes 13k aiohttp Yes No Yes 15k Test setup import time import requests import httpx import aiohttp import asyncio URL = ' https://httpbin.org/get ' N = 100 # requests per test def bench_requests (): start = time . time () session = requests . Session () for _ in range ( N ): session . get ( URL ) return time . time () - start def bench_httpx_sync (): start = time . time () with httpx . Client () as client : for _ in range ( N ): client . get ( URL ) return time . time () - start async def bench_httpx_async (): start = time . time () async with httpx . AsyncClient () as client : tasks = [ client . get ( URL ) for _ in range ( N )] await asyncio . gather ( * tasks ) retu

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
2 views

Related Articles