Advertisement
collinsanele

Threads in Python

Sep 12th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. import requests
  2. import time
  3. import concurrent.futures
  4.  
  5. img_urls = ['https://images.unsplash.com/photo-1516117172878-fd2c41f4a759', 'https://images.unsplash.com/photo-1522364723953-452d3431c267']
  6.  
  7.  
  8. def download_image(img_url):
  9.   img_bytes = requests.get(img_url).content
  10.   img_name = img_url.split('/')[3]
  11.   img_name = f'{img_name}.jpg'
  12.  
  13.   with open(img_name, 'wb') as img_file:
  14.     img_file.write(img_bytes)
  15.     print(f'{img_name} was downloaded...')
  16.  
  17. with concurrent.futures.ThreadPoolExecutor() as executor:
  18.   executor.map(download_image, img_urls)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement