Last active
December 3, 2021 16:53
-
-
Save dgtlctzn/361e8db34c92f034322f2d363c8679c9 to your computer and use it in GitHub Desktop.
All Together Now
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
import asyncio | |
import aiohttp | |
CRYPTOS: List[str] = [ | |
"BTC", | |
"ETH", | |
"DOGE", | |
"BCH", | |
"ETC", | |
"LTC", | |
] | |
URLS: List[str] = [ | |
f"https://api.coinbase.com/v2/prices/{crypto}-USD/buy" | |
for crypto in CRYPTOS | |
] | |
async def request_urls(urls: List[str]): | |
async with aiohttp.ClientSession() as session: | |
tasks: List[asyncio.Task] = [] | |
for url in urls: | |
tasks.append( | |
asyncio.ensure_future( | |
get_url(session, url) | |
) | |
) | |
return await asyncio.gather(*tasks) | |
async def get_url(session: aiohttp.ClientSession, url: str) -> Dict: | |
async with session.get(url) as response: | |
return await response.json() | |
responses: List[Dict] = asyncio.run(request_urls(URLS)) | |
print(responses) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment