Last active
May 9, 2022 00:28
-
-
Save cgarciae/364de29545d79fafa4e6a993a30397de to your computer and use it in GitHub Desktop.
client-async-sem.py
This file contains hidden or 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
# client-async-sem.py | |
from aiohttp import ClientSession, TCPConnector | |
import asyncio | |
import sys | |
limit = 1000 | |
async def fetch(url, session): | |
async with session.get(url) as response: | |
return await response.read() | |
async def bound_fetch(sem, url, session): | |
# Getter function with semaphore. | |
async with sem: | |
await fetch(url, session) | |
async def run(session, r): | |
url = "http://localhost:8080/{}" | |
tasks = [] | |
# create instance of Semaphore | |
sem = asyncio.Semaphore(limit) | |
for i in range(r): | |
# pass Semaphore and session to every GET request | |
task = asyncio.ensure_future(bound_fetch(sem, url.format(i), session)) | |
tasks.append(task) | |
responses = asyncio.gather(*tasks) | |
await responses | |
loop = asyncio.get_event_loop() | |
async def main(): | |
connector = TCPConnector(limit=None) | |
async with ClientSession(connector=connector) as session: | |
await run(session, int(sys.argv[1])) | |
loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment