Last active
June 26, 2023 06:08
-
-
Save code-yeongyu/3ef3538521ccd4d781dc63c6bc431f88 to your computer and use it in GitHub Desktop.
aiohttp vs httpx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import time | |
import httpx | |
from aiohttp import ClientSession | |
async def fetch_httpx(url: str) -> None: | |
async with httpx.AsyncClient() as client: | |
await client.get(url) | |
async def fetch_aiohttp(url: str, session: ClientSession) -> None: | |
async with session.get(url): | |
pass | |
async def run_httpx(urls: list[str]) -> None: | |
tasks = [fetch_httpx(url) for url in urls] | |
await asyncio.gather(*tasks) | |
async def run_aiohttp(urls: list[str]) -> None: | |
async with ClientSession() as session: | |
tasks = [fetch_aiohttp(url, session) for url in urls] | |
await asyncio.gather(*tasks) | |
def main() -> None: | |
urls = ["https://www.google.com/"] * 100 | |
start = time.time() | |
asyncio.run(run_httpx(urls)) | |
httpx_time = time.time() - start | |
start = time.time() | |
asyncio.run(run_aiohttp(urls)) | |
aiohttp_time = time.time() - start | |
print(f"HTTPX time: {httpx_time}") | |
print(f"AIOHTTP time: {aiohttp_time}") | |
if __name__ == "__main__": | |
main() | |
# HTTPX time: 1.0568580627441406 | |
# AIOHTTP time: 0.4790208339691162 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment