Skip to content

Instantly share code, notes, and snippets.

@manashmandal
Created April 28, 2020 06:16
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 manashmandal/c9c9ce616a2c5a488b3a01b08ce884d7 to your computer and use it in GitHub Desktop.
Save manashmandal/c9c9ce616a2c5a488b3a01b08ce884d7 to your computer and use it in GitHub Desktop.
Asyncio Concurrent Request
# Need at least Python 3.7 To Run this code
import asyncio
import aiohttp
endpoint = "https://randomuser.me/api/"
num_concurrent_requests = 20
async def fetch(session, url):
async with session.get(url) as response:
return await response.json()
async def main():
async with aiohttp.ClientSession() as session:
results = await asyncio.gather(
*(fetch(session, endpoint) for _ in range(num_concurrent_requests))
)
print(results)
asyncio.run(main())
aiohttp==3.6.2
appdirs==1.4.3
async-timeout==3.0.1
attrs==19.3.0
black==19.10b0
chardet==3.0.4
click==7.1.2
idna==2.9
multidict==4.7.5
pathspec==0.8.0
regex==2020.4.4
toml==0.10.0
typed-ast==1.4.1
yarl==1.4.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment