Concurrency-With-Python
Concurrency-With-Python copied to clipboard
Chap 1: lorempixel not working
Lorempixel's endpoint seems out. Here's a functional code for sequential download:
import urllib.request
from os import mkdir
import time
SAVEPATH = "tmp/"
URL = "https://picsum.photos/200/300"
NUM_OF_IMAGES = 4
try:
mkdir(SAVEPATH)
except:
pass
def download_img(img_path, filename):
print("Downloading image from ", img_path)
urllib.request.urlretrieve(img_path, filename)
def main():
t0 = time.time()
for i in range(NUM_OF_IMAGES):
img_name = SAVEPATH + "image-" + str(i) + ".jpg"
download_img(URL, img_name)
total_time = time.time() - t0
print(f"Total execution time: {total_time}")
if __name__ == '__main__':
main()