import requests from multiprocessing import Pool def download_image(url): response = requests.get(url) print("Downloading image from", url) return response.content if __name__ == "__main__": urls = ['https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg', 'https://cdn.pixabay.com/photo/2015/05/15/14/38/computer-768608__480.jpg', 'https://cdn.pixabay.com/photo/2015/05/15/14/39/mobile-phone-768608__480.jpg'] pool = Pool(processes=3) results = pool.imap(download_image, urls) for result in results: print(len(result))
from multiprocessing import Pool def factorial(n): result = 1 for i in range(1, n+1): result *= i return result if __name__ == "__main__": numbers = [5, 10, 15, 20] pool = Pool(processes=2) results = pool.imap(factorial, numbers) for result in results: print(result)In the first example, we download multiple images from the given urls concurrently using the pool that executes three download_image() functions at a time. The results iterator is then printed which prints the size of each image file. In the second example, we calculate factorial of each number in the list concurrently using the pool that executes two factorial() functions at a time. The results iterator is then printed which prints the factorials of each number. To implement the multiprocessing.Pool.imap() function, we would need to import the multiprocessing package library as given in the above examples.