From the course: Python Parallel and Concurrent Programming Part 2

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Download images in Python

Solution: Download images in Python - Python Tutorial

From the course: Python Parallel and Concurrent Programming Part 2

Start my 1-month free trial

Solution: Download images in Python

(upbeat music) - [Man] For our solution to the download images challenge, we considered each of the images that needed to be downloaded as separate tasks, which would each be a call to the download image helper function. Since we would need to get the result value, for the number of bytes from each of these tasks, it seemed like a perfect use case for futures, with each task being created as a callable object, to be executed by a thread pool. So, that's what we did. Line 33 of our implementation of the parallel download images function, establishes a new thread pool, with the default settings. So, it will have up to five times as many threads, as there are processing cores in our machine. On the next line, we use a Python list comprehension, to submit all of the image numbers as tasks for the download image function, and store the returned future objects in a list. The for loop on line 35 then retrieves those future objects from the list, as they complete, retrieves the result's…

Contents