Skip to content

Instantly share code, notes, and snippets.

@asolera
Last active January 29, 2021 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asolera/4ff7dedf6c3b646e93830dea57719011 to your computer and use it in GitHub Desktop.
Save asolera/4ff7dedf6c3b646e93830dea57719011 to your computer and use it in GitHub Desktop.
Parallel (async) HTTP requests with configurable concurrency using Python/aiohttp/asyncio
import aiohttp
import nest_asyncio
nest_asyncio.apply()
from random import randint
class HttpClient:
def __init__(self, session, concurrency):
self.__session = session
self.concurrency = asyncio.Semaphore(concurrency)
async def download(self, url):
print(datetime.now(), url, 'downloading')
async with self.__session.get(url) as response:
print(datetime.now(), url, response.status, 'downloaded')
return response.status
async def safe_download(self, url):
async with self.concurrency:
return await self.download(url)
async def main(urls):
async with aiohttp.ClientSession() as session:
http_client = HttpClient(session=session, concurrency=4)
tasks = [asyncio.ensure_future(http_client.safe_download(url)) for url in urls]
await asyncio.gather(*tasks)
urls = []
for i in range(10):
rand = randint(2000, 5000)
url = "https://deelay.me/{}/http://httpbin.org/get".format(rand)
urls.append(url)
print(urls)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main(urls))
finally:
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment