Skip to content

Instantly share code, notes, and snippets.

@dhrubabasu
Created October 6, 2018 03:15
Show Gist options
  • Save dhrubabasu/396e94ec29ab3ad6763d34d7e6d743ed to your computer and use it in GitHub Desktop.
Save dhrubabasu/396e94ec29ab3ad6763d34d7e6d743ed to your computer and use it in GitHub Desktop.
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