From 42022288cebc70e249ae0b979f500172be860d24 Mon Sep 17 00:00:00 2001 From: "Brandon C. Irizarry" Date: Fri, 17 Apr 2026 18:33:12 -0400 Subject: refactor: wrap the fetching-task with the semaphore 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". --- main.py | 14 +++++++++++--- 1 file 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] -- cgit v1.2.3