diff options
| -rw-r--r-- | main.py | 14 |
1 files changed, 11 insertions, 3 deletions
@@ -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] |
