66import stat
77import tempfile
88import uuid
9- from concurrent .futures import ThreadPoolExecutor , as_completed
9+ from concurrent .futures import Future , ThreadPoolExecutor , as_completed
1010from functools import partial
1111from pathlib import Path
12- from typing import Any , Dict , List , Tuple
12+ from typing import Any , BinaryIO , Dict , List , Tuple
1313
1414import requests
1515from filelock import FileLock
@@ -513,6 +513,18 @@ def _initiate_upload(
513513
514514 return response .data
515515
516+ def _submit_part (
517+ self ,
518+ executor : ThreadPoolExecutor ,
519+ f : BinaryIO ,
520+ part_info : Dict [str , Any ],
521+ part_size : int ,
522+ ) -> Future [str ]:
523+ """Submit a single part for upload and return the future"""
524+ f .seek ((part_info ["PartNumber" ] - 1 ) * part_size )
525+ part_data = f .read (part_size )
526+ return executor .submit (self ._upload_single_part , part_info , part_data )
527+
516528 def _upload_parts_concurrent (
517529 self , file : Path , upload_info : Dict [str , Any ], part_size : int
518530 ) -> List [Dict [str , Any ]]:
@@ -530,12 +542,7 @@ def _upload_parts_concurrent(
530542 # Submit initial batch limited by max_concurrent_parts
531543 for i in range (min (self .max_concurrent_parts , len (parts ))):
532544 part_info = parts [part_index ]
533- f .seek ((part_info ["PartNumber" ] - 1 ) * part_size )
534- part_data = f .read (part_size )
535-
536- future = executor .submit (
537- self ._upload_single_part , part_info , part_data
538- )
545+ future = self ._submit_part (executor , f , part_info , part_size )
539546 future_to_part [future ] = part_info ["PartNumber" ]
540547 part_index += 1
541548
@@ -556,11 +563,8 @@ def _upload_parts_concurrent(
556563 # Submit next part if available
557564 if part_index < len (parts ):
558565 part_info = parts [part_index ]
559- f .seek ((part_info ["PartNumber" ] - 1 ) * part_size )
560- part_data = f .read (part_size )
561-
562- future = executor .submit (
563- self ._upload_single_part , part_info , part_data
566+ future = self ._submit_part (
567+ executor , f , part_info , part_size
564568 )
565569 future_to_part [future ] = part_info ["PartNumber" ]
566570 part_index += 1
0 commit comments