summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrandon C. Irizarry <brandon.irizarry@gmail.com>2026-04-16 17:04:45 -0400
committerBrandon C. Irizarry <brandon.irizarry@gmail.com>2026-04-16 17:04:45 -0400
commitaa716390836e5e58d9bf3987b24be3f2a4110245 (patch)
tree9608c1cb5696390de98de3651c11af4ee65e80a0
parente771a3ebcee9a7c6c3efd08fa905cbe0591131b1 (diff)
feat: use full concurrency (no semaphores)
-rw-r--r--main.py33
1 files changed, 18 insertions, 15 deletions
diff --git a/main.py b/main.py
index d874d42..f653a7d 100644
--- a/main.py
+++ b/main.py
@@ -33,7 +33,18 @@ def get_urls(filename: str, limit: int | None = None) -> list[str]:
return urls
-async def ping(urls: list[str], max_concurrency=None) -> None:
+async def fetch(session: aiohttp.ClientSession, url: str) -> str:
+ try:
+ async with session.get(url) as response:
+ if response.ok:
+ return "😁"
+ else:
+ return "😓"
+ except aiohttp.ClientError:
+ return "🤮"
+
+
+async def ping(urls: list[str], max_concurrency=None) -> list[str]:
"""Make a GET request to members of URLS.
If MAX_CONCURRENCY is None, browse every site at once.
@@ -44,19 +55,11 @@ async def ping(urls: list[str], max_concurrency=None) -> None:
"""
- async with aiohttp.ClientSession(max_field_size=8190 * 2, timeout=1.0) as session:
- for url in urls:
- try:
- async with session.get(url) as response:
- print(f"Status: {response.status}")
-
- if not response.ok:
- print(f"Got code {response.status} from URL; skipping")
- continue
-
- print(f"Content-Type: {response.headers['content-type']}")
- except aiohttp.ClientError as e:
- print(f"Something bad happened with URL: {e}; skipping")
+ async with aiohttp.ClientSession(
+ max_field_size=8190 * 2, timeout=aiohttp.ClientTimeout(30)
+ ) as session:
+ tasks = [fetch(session, url) for url in urls]
+ return await asyncio.gather(*tasks)
def main():
@@ -68,7 +71,7 @@ def main():
urls = get_urls("majestic_million.csv", limit)
start_time = time.perf_counter()
- asyncio.run(ping(urls))
+ print(asyncio.run(ping(urls)))
end_time = time.perf_counter()
elapsed_time = end_time - start_time