Last active
June 4, 2019 20:24
-
-
Save kuntalchandra/73d79df0c16227817cd2a447c6031a7b to your computer and use it in GitHub Desktop.
Parallel processing using multiprocessing and multithreading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import concurrent.futures | |
from PIL import Image | |
from resizeimage import resizeimage | |
from os import listdir, getpid | |
from os.path import isfile, join | |
SOURCE_DIR = '/var/tmp/source/' | |
TARGET_DIR = '/var/tmp/target/' | |
MAX_PROCESSES = 4 | |
MAX_THREADS = 2 | |
class ImageResizer(object): | |
def __init__(self): | |
self.sizes = [[100, 100], [200, 200], [300, 300], [400, 400], [500, 500], [600, 600], [700, 700], [800, 800], [900, 900], [1000, 1000]] | |
def execute(self): | |
images = self.get_images() | |
self.execute_process(images) | |
@staticmethod | |
def get_images(): | |
return [file for file in listdir(SOURCE_DIR) if isfile(join(SOURCE_DIR, file))] | |
def execute_process(self, images: list): | |
with concurrent.futures.ProcessPoolExecutor(max_workers=MAX_PROCESSES) as process_executor: | |
process_futures = process_executor.map(self.resize_image, [(image) for image in images]) | |
for processed_images in process_futures: | |
print("Resized images: {}".format(", ".join(image for image in processed_images))) | |
def resize_image(self, image: str): | |
pid = getpid() | |
print("Running task from process {}".format(pid)) | |
with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_THREADS) as thread_executor: | |
resized_images = [] | |
thread_futures = thread_executor.map(self.resize, [(image, size) for size in self.sizes]) | |
for resized_image in thread_futures: | |
resized_images.append(resized_image) | |
return resized_images | |
@staticmethod | |
def resize(args): | |
file, size = args | |
with Image.open(SOURCE_DIR + file) as image: | |
cover = resizeimage.resize_cover(image, size) | |
resized_image = TARGET_DIR + '-'.join(str(s) for s in size) + file | |
cover.save(resized_image, image.format) | |
return resized_image | |
if __name__ == "__main__": | |
image_resizer = ImageResizer() | |
image_resizer.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment