summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrandon C. Irizarry <brandon.irizarry@gmail.com>2026-04-17 18:33:12 -0400
committerBrandon C. Irizarry <brandon.irizarry@gmail.com>2026-04-17 18:33:12 -0400
commit42022288cebc70e249ae0b979f500172be860d24 (patch)
treeb882aa1c2fcb40215b6e95133b29a414d1d74c3c
parent48a32e67c8061c6d60cc1afe1547574e34885f5c (diff)
refactor: wrap the fetching-task with the semaphoreHEADmain
That is, do that instead of wrapping the code inside 'fetch'. The current approach conveys more pithily what our intention is - that 'fetch' is the entirety of the asynchronous task we want to "rate limit".
-rw-r--r--main.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/main.py b/main.py
index cf647ff..44662b2 100644
--- a/main.py
+++ b/main.py
@@ -33,7 +33,7 @@ def get_urls(filename: str, limit: int | None = None) -> list[str]:
return urls
-async def fetch(session: aiohttp.ClientSession, url: str, semaphore: asyncio.Semaphore) -> str:
+async def fetch(session: aiohttp.ClientSession, url: str) -> str:
"""Fetch a URL under the given SESSION.
Use SEMAPHORE to limit the number of tasks that can make an HTTP
@@ -45,7 +45,7 @@ async def fetch(session: aiohttp.ClientSession, url: str, semaphore: asyncio.Sem
"""
try:
- async with semaphore, session.get(url) as response:
+ async with session.get(url) as response:
print(".", end="", flush=True)
if response.ok:
return "😁"
@@ -73,7 +73,15 @@ async def ping(urls: list[str], max_concurrency: int) -> list[str]:
asyncio.TaskGroup() as tg,
):
semaphore = asyncio.Semaphore(max_concurrency)
- tasks = [tg.create_task(fetch(session, url, semaphore)) for url in urls]
+ tasks: list[asyncio.Task[str]] = []
+
+ for url in urls:
+ # Wrap the fetch-call with the semaphore.
+ async def rate_limited():
+ async with semaphore:
+ return await fetch(session, url)
+
+ tasks.append(tg.create_task(rate_limited()))
return [t.result() for t in tasks]