From e7aafdb917b26af81649b2eda98ff12738cd6460 Mon Sep 17 00:00:00 2001 From: Andrew Macmillan Date: Thu, 31 Dec 2020 12:31:45 +0000 Subject: [PATCH] record and compare time to make 10 API requests --- single_thread_vs_multi_thread/main.py | 67 +++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 single_thread_vs_multi_thread/main.py diff --git a/single_thread_vs_multi_thread/main.py b/single_thread_vs_multi_thread/main.py new file mode 100644 index 0000000..d5fcb0e --- /dev/null +++ b/single_thread_vs_multi_thread/main.py @@ -0,0 +1,67 @@ +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()