Skip to content

Instantly share code, notes, and snippets.

@Airbus5717
Created November 20, 2021 14:41
Show Gist options
  • Save Airbus5717/38d370986d49990bd9aaab795ad529ba to your computer and use it in GitHub Desktop.
Save Airbus5717/38d370986d49990bd9aaab795ad529ba to your computer and use it in GitHub Desktop.
Punish
import random
import asyncio
from aiohttp import ClientSession
async def fetch(url, session):
async with session.get(url) as response:
delay = response.headers.get("DELAY")
date = response.headers.get("DATE")
print("{}:{} with delay {}".format(date, response.url, delay))
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(r):
# url = ""
tasks = []
# create instance of Semaphore
sem = asyncio.Semaphore(1000)
# Create client session that will ensure we dont open new connection
# per each request.
async with ClientSession() as session:
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
while True:
number = 100000
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