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", |
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
$ python3 async_crypto.py | |
[{'data': {'base': 'BTC', 'currency': 'USD', 'amount': '58995.78'}}, | |
{'data': {'base': 'ETH', 'currency': 'USD', 'amount': '4467.16'}}, | |
{'data': {'base': 'DOGE', 'currency': 'USD', 'amount': '0.22'}}, | |
{'data': {'base': 'BCH', 'currency': 'USD', 'amount': '582.49'}}, | |
{'data': {'base': 'ETC', 'currency': 'USD', 'amount': '48.59'}}, | |
{'data': {'base': 'LTC', 'currency': 'USD', 'amount': '208.84'}}] |
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
from time import time | |
responses: List[Dict] = [] | |
start_time: float = time() | |
for url in URLS: | |
responses.append(requests.get(url).json()) | |
end_time: float = time() |
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
responses: List[Dict] = asyncio.run(request_urls(urls)) |
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
async def request_urls(urls: List[str]): | |
tasks: List[asyncio.Task] = [] | |
... | |
return await asyncio.gather(*tasks) |
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
task: asyncio.Task = asyncio.ensure_future( | |
get_url(session, url) | |
) |
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
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) |
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
async with session.post(url, data=payload) as response: | |
... |
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
... | |
async with session.get(url) as response: | |
if response.status == 503: | |
# do some work |
OlderNewer