diff options
| author | Brandon C. Irizarry <brandon.irizarry@gmail.com> | 2026-04-16 17:34:36 -0400 |
|---|---|---|
| committer | Brandon C. Irizarry <brandon.irizarry@gmail.com> | 2026-04-16 17:34:36 -0400 |
| commit | 28bb531bd6eec1a9139944fd5b5d1d05a08becd2 (patch) | |
| tree | 47edc1a06d994b235cec710ad6dbb0b01cc6a616 /main.py | |
| parent | a6468fc6a28adc628e64c09ef1e953b8eaf0807a (diff) | |
feat: use a semaphore to limit how many tasks make requests
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 9 |
1 files changed, 5 insertions, 4 deletions
@@ -33,9 +33,9 @@ def get_urls(filename: str, limit: int | None = None) -> list[str]: return urls -async def fetch(session: aiohttp.ClientSession, url: str) -> str: +async def fetch(session: aiohttp.ClientSession, url: str, semaphore: asyncio.Semaphore) -> str: try: - async with session.get(url) as response: + async with semaphore, session.get(url) as response: print(".", end="", flush=True) if response.ok: return "😁" @@ -47,7 +47,7 @@ async def fetch(session: aiohttp.ClientSession, url: str) -> str: return "😴" -async def ping(urls: list[str], max_concurrency=None) -> list[str]: +async def ping(urls: list[str], max_concurrency: int = 1) -> list[str]: """Make a GET request to members of URLS. If MAX_CONCURRENCY is None, browse every site at once. @@ -62,7 +62,8 @@ async def ping(urls: list[str], max_concurrency=None) -> list[str]: aiohttp.ClientSession(max_field_size=8190 * 2, timeout=aiohttp.ClientTimeout(5)) as session, asyncio.TaskGroup() as tg, ): - tasks = [tg.create_task(fetch(session, url)) for url in urls] + semaphore = asyncio.Semaphore(max_concurrency) + tasks = [tg.create_task(fetch(session, url, semaphore)) for url in urls] return [t.result() for t in tasks] |
