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
$ pip install aiohttp |
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 get_url(session: aiohttp.ClientSession, url: str) -> Dict: | |
async with session.get(url) as response: | |
return await response.json() |
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 |
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 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
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]): | |
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
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
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() |
NewerOlder