Skip to content

Commit 77b972a

Browse files
committed
Switched to using ThreadPoolExecutor
1 parent 4feb1b6 commit 77b972a

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

combineImg/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
FROM public.ecr.aws/lambda/python:3.8
2+
RUN yum -y update
23
RUN yum -y install git && yum clean all
34
RUN yum -y install tar gzip zlib freetype-devel \
45
gcc \

combineImg/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
from pickletools import optimize
2-
from turtle import down
3-
from weakref import finalize
41
from PIL import Image, ImageOps, ImageFilter
52
from requests import get
63
from io import BytesIO
74
import base64
8-
from multiprocessing.pool import ThreadPool
5+
import concurrent.futures
96
from time import time as timer
107

118
# find the highest res image in an array of images
@@ -42,7 +39,9 @@ def scaleImageIterable(args):
4239

4340
def scaleAllImagesToSameSize(imageArray,targetWidth,targetHeight,pad=True): # scale all images in the array to the same size, preserving aspect ratio
4441
newImageArray = []
45-
newImageArray=[scaleImageIterable([image,targetWidth,targetHeight,pad]) for image in imageArray]
42+
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
43+
newImageArray = [executor.submit(scaleImageIterable, (image, targetWidth, targetHeight,pad)) for image in imageArray]
44+
newImageArray = [future.result() for future in newImageArray]
4645
return newImageArray
4746

4847
def blurImage(image, radius):
@@ -107,15 +106,16 @@ def genImageFromURL(urlArray):
107106
# no cache means that they'll have to be downloaded again if the image is requested again
108107
# TODO: cache?
109108
start = timer()
110-
imageArray = ThreadPool(8).map(downloadImage,urlArray)
109+
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
110+
imageArray = [executor.submit(downloadImage, url) for url in urlArray]
111+
imageArray = [future.result() for future in imageArray]
111112
print(f"Images downloaded in: {timer() - start}s")
112113
start = timer()
113114
finalImg = genImage(imageArray)
114115
print(f"Image generated in: {timer() - start}s")
115116
return finalImg
116117

117118
def lambda_handler(event, context):
118-
# TODO implement
119119
images = event["queryStringParameters"].get("imgs","").split(",")
120120
combined = genImageFromURL(images)
121121
buffered = BytesIO()

0 commit comments

Comments
 (0)