Created
October 6, 2018 03:15
-
-
Save dhrubabasu/396e94ec29ab3ad6763d34d7e6d743ed to your computer and use it in GitHub Desktop.
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
import sys | |
import asyncio | |
from aiohttp import ClientSession | |
async def fetch(url, session): | |
async with session.get(url) as response: | |
response = await response.read() | |
async def limit_fetch(sem, url, session): | |
async with sem: | |
await fetch(url, session) | |
async def run(r): | |
url = "https://httpbin.org/get/{}" | |
tasks = [] | |
sem = asyncio.Semaphore(1000) | |
async with ClientSession() as session: | |
for i in range(r): | |
task = asyncio.ensure_future(limit_fetch(sem, url.format(i), session)) | |
tasks.append(task) | |
responses = asyncio.gather(*tasks) | |
await responses | |
number = int(sys.argv[1]) | |
loop = asyncio.get_event_loop() | |
future = asyncio.ensure_future(run(number)) | |
loop.run_until_complete(future) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment