Skip to content

Commit bcd4e75

Browse files
committed
fix: parallel upload
1 parent 5958a6b commit bcd4e75

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

runpod/serverless/utils/upload.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
# ---------------------------------------------------------------------------- #
3737
# Upload Image #
3838
# ---------------------------------------------------------------------------- #
39-
def upload_image(job_id, job_result, result_index=0):
39+
def upload_image(job_id, image_location, result_index=0, results_list=None):
4040
'''
4141
Upload image to bucket storage.
4242
'''
4343
if boto_client is None:
4444
# Save the output to a file
4545
output = BytesIO()
46-
img = Image.open(job_result)
46+
img = Image.open(image_location)
4747
img.save(output, format=img.format)
4848

4949
os.makedirs("uploaded", exist_ok=True)
@@ -53,7 +53,7 @@ def upload_image(job_id, job_result, result_index=0):
5353
return f"uploaded/{result_index}.png"
5454

5555
output = BytesIO()
56-
img = Image.open(job_result)
56+
img = Image.open(image_location)
5757
img.save(output, format=img.format)
5858

5959
bucket = time.strftime('%m-%y')
@@ -75,6 +75,9 @@ def upload_image(job_id, job_result, result_index=0):
7575
'Key': f'{job_id}/{result_index}.png'
7676
}, ExpiresIn=604800)
7777

78+
if results_list is not None:
79+
results_list[result_index] = presigned_url
80+
7881
return presigned_url
7982

8083

@@ -86,18 +89,20 @@ def files(job_id, file_list):
8689
Uploads a list of files in parallel.
8790
Once all files are uploaded, the function returns the presigned URLs list.
8891
'''
89-
upload_progress = []
92+
upload_progress = [] # List of threads
93+
file_urls = [None] * len(file_list) # Resulting list of URLs for each file
9094

9195
for index, file in enumerate(file_list):
9296
new_upload = threading.Thread(
9397
target=upload_image,
94-
args=(job_id, file, index)
98+
args=(job_id, file, index, file_urls)
9599
)
96100

97101
new_upload.start()
98102
upload_progress.append(new_upload)
99103

104+
# Wait for all uploads to finish
100105
for upload in upload_progress:
101106
upload.join()
102107

103-
return upload_progress
108+
return file_urls

0 commit comments

Comments
 (0)