Created
April 28, 2020 06:16
-
-
Save manashmandal/c9c9ce616a2c5a488b3a01b08ce884d7 to your computer and use it in GitHub Desktop.
Asyncio Concurrent Request
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
# 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()) |
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
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