# /// script # requires-python = ">=3.12" # dependencies = [ # "httpx", # ] # /// import asyncio import time from collections import Counter from httpx import AsyncClient status_counter: Counter[int] = Counter() async def worker(client: AsyncClient, queue: asyncio.Queue[int]): while True: i = await queue.get() r = await client.get( f'https://cloudflare.com/cdn-cgi/trace?v={i}', ) status_counter[r.status_code] += 1 queue.task_done() async def main(): queue: asyncio.Queue[int] = asyncio.Queue() requests = 10_000 for i in range(requests): queue.put_nowait(i) tasks: list[asyncio.Task[None]] = [] async with AsyncClient() as client: for i in range(100): tasks.append(asyncio.create_task(worker(client, queue))) started_at = time.perf_counter() await queue.join() time_taken = time.perf_counter() - started_at for task in tasks: task.cancel() try: await asyncio.gather(*tasks) except asyncio.CancelledError: pass print(f'time taken to make {requests} requests: {time_taken:.4f} seconds') print(status_counter) if __name__ == '__main__': asyncio.run(main())