Skip to content

Instantly share code, notes, and snippets.

@dgtlctzn
Last active December 3, 2021 16:53
Show Gist options
  • Save dgtlctzn/361e8db34c92f034322f2d363c8679c9 to your computer and use it in GitHub Desktop.
Save dgtlctzn/361e8db34c92f034322f2d363c8679c9 to your computer and use it in GitHub Desktop.
All Together Now
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