concurrency_in_python/single_thread_vs_multi_thread/main.py

68 lines
1.8 KiB
Python

import requests
from time import time
from threading import Thread
# This example shows how downloading multiple images from the
# internet can be sped up using multiple threads. We are just
# getting JSON responses and recording the time taken in each
# example
# Images are being download from the pexels.com API
from pexels_api_key import API_KEY
api_url = 'https://api.pexels.com/v1/search?query='
queries = ['people', 'cars', 'landscpapes', 'pets',
'tech', 'food', 'books', 'sports', 'games', 'plants']
def download_images(query):
full_url = f'{api_url}{query}'
headers = {'Authorization': API_KEY}
requests.get(full_url, data=None, headers=headers)
# Sequential Example
def record_seq_time():
t0 = time()
print('\nDownloading images sequentially')
for query in queries:
download_images(query=query)
t1 = time()
print('Download complete')
return f'{t1-t0:.2f}s'
# Threaded example - no need to wait for a response before sending the next request
def record_con_time():
t0 = time()
threads = []
print('\nDownloading images using multiple threads')
for query in queries:
thread = Thread(target=download_images, args=(query,))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
t1 = time()
print('Download complete')
return f'{t1-t0:.2f}'
def main():
seq_time = record_seq_time()
con_time = record_con_time()
print('\n=======================================================')
print(f'Sequential download completed in {seq_time}')
print(f'Multi-threaded download completed in {con_time}') # this is significantly faster
print('=======================================================')
if __name__ == '__main__':
main()