From 45449e0b568347d0532bfca24abb0782d9616178 Mon Sep 17 00:00:00 2001 From: eduard0803 Date: Thu, 17 Oct 2024 12:18:23 -0300 Subject: [PATCH] adding script to send async requests --- arequest.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 arequest.py diff --git a/arequest.py b/arequest.py new file mode 100644 index 0000000..ce3f2e9 --- /dev/null +++ b/arequest.py @@ -0,0 +1,28 @@ +from time import perf_counter +import asyncio + +import aiohttp + + +async def fetch(s, url): + async with s.get("http://localhost:8000") as r: + return await r.text() + +async def fetch_all(s, urls): + tasks = [] + for url in urls: + task = asyncio.create_task(fetch(s, url)) + tasks.append(task) + return await asyncio.gather(*tasks) + +async def main(): + urls = range(1, 10 * 1000) + async with aiohttp.ClientSession() as s: + html = await fetch_all(s, urls) + print(html) + +if __name__ == "__main__": + start = perf_counter() + asyncio.run(main()) + end = perf_counter() + print(f"time: {end-start}")