Created
November 17, 2019 12:18
-
-
Save larrycai/cc6006eff8ca3434a4b078457c85d81f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/bin/env python | |
# https://towardsdatascience.com/stop-waiting-start-using-async-and-await-18fcd1c28fd0 | |
# docker run -it -v $PWD:/src -w /src python:3.7-slim | |
# pip install aiohttp asyncio requests | |
import aiohttp | |
import asyncio | |
import time | |
from typing import Any, Iterable, List, Tuple, Callable | |
import os | |
import requests | |
def image_name_from_url(url: str) -> str: | |
return url.split("/")[-1] | |
def download_all(urls: Iterable[str]) -> List[Tuple[str, bytes]]: | |
def download(url: str) -> Tuple[str, bytes]: | |
print(f"Start downloading {url}") | |
with requests.Session() as s: | |
resp = s.get(url) | |
out= image_name_from_url(url), resp.content | |
print(f"Done downloading {url}") | |
return out | |
return [download(url) for url in urls] | |
async def donwload_aio(urls:Iterable[str])->List[Tuple[str, bytes]]: | |
async def download(url: str) -> Tuple[str, bytes]: | |
print(f"Start downloading {url}") | |
async with aiohttp.ClientSession() as s: | |
resp = await s.get(url) | |
out = image_name_from_url(url), await resp.read() | |
print(f"Done downloading {url}") | |
return out | |
return await asyncio.gather(*[download(url) for url in urls]) | |
if __name__ == "__main__": | |
# Get list of images from dogs API | |
URL = "https://dog.ceo/api/breed/hound/images" | |
images = requests.get(URL).json()["message"] | |
# Take only 200 images to not run into issues with gather | |
reduced = images[:200] | |
st = time.time() | |
images_s = download_all(reduced) | |
print(f"Synchronous exec took {time.time() - st} seconds") | |
st = time.time() | |
images_a = asyncio.run(donwload_aio(reduced)) | |
print(f"Asynchronous exec took {time.time() - st} seconds") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment