-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding script to send async requests
- Loading branch information
1 parent
39d8b86
commit 45449e0
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}") |