diff options
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 19 |
1 files changed, 13 insertions, 6 deletions
@@ -36,12 +36,15 @@ def get_urls(filename: str, limit: int | None = None) -> list[str]: async def fetch(session: aiohttp.ClientSession, url: str) -> str: try: async with session.get(url) as response: + print(".", end="", flush=True) if response.ok: return "😁" else: return "😓" except aiohttp.ClientError: return "🤮" + except TimeoutError: + return "😴" async def ping(urls: list[str], max_concurrency=None) -> list[str]: @@ -55,11 +58,13 @@ async def ping(urls: list[str], max_concurrency=None) -> list[str]: """ - 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) + async with ( + 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] + + return [t.result() for t in tasks] def main(): @@ -71,7 +76,9 @@ def main(): urls = get_urls("majestic_million.csv", limit) start_time = time.perf_counter() - print(asyncio.run(ping(urls))) + results = asyncio.run(ping(urls)) + print() + print(results) end_time = time.perf_counter() elapsed_time = end_time - start_time |
