Skip to content

Commit c95fb07

Browse files
authored
[Storage] Fix low performance of async download routines (#33499)
1 parent fd8cd1f commit c95fb07

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

sdk/storage/azure-storage-blob/azure/storage/blob/aio/_download_async.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,11 @@ async def read(self, size: Optional[int] = -1) -> T:
527527
except HttpResponseError as error:
528528
process_storage_error(error)
529529
try:
530-
next_chunk = next(dl_tasks)
530+
for _ in range(0, len(done)):
531+
next_chunk = next(dl_tasks)
532+
running_futures.add(asyncio.ensure_future(downloader.process_chunk(next_chunk)))
531533
except StopIteration:
532534
break
533-
else:
534-
running_futures.add(asyncio.ensure_future(downloader.process_chunk(next_chunk)))
535535

536536
if running_futures:
537537
# Wait for the remaining downloads to finish
@@ -683,11 +683,11 @@ async def readinto(self, stream: IO[T]) -> int:
683683
except HttpResponseError as error:
684684
process_storage_error(error)
685685
try:
686-
next_chunk = next(dl_tasks)
686+
for _ in range(0, len(done)):
687+
next_chunk = next(dl_tasks)
688+
running_futures.add(asyncio.ensure_future(downloader.process_chunk(next_chunk)))
687689
except StopIteration:
688690
break
689-
else:
690-
running_futures.add(asyncio.ensure_future(downloader.process_chunk(next_chunk)))
691691

692692
if running_futures:
693693
# Wait for the remaining downloads to finish

sdk/storage/azure-storage-file-share/azure/storage/fileshare/aio/_download_async.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,18 +445,28 @@ async def readinto(self, stream):
445445
]
446446
while running_futures:
447447
# Wait for some download to finish before adding a new one
448-
_done, running_futures = await asyncio.wait(
448+
done, running_futures = await asyncio.wait(
449449
running_futures, return_when=asyncio.FIRST_COMPLETED)
450450
try:
451-
next_chunk = next(dl_tasks)
451+
for task in done:
452+
task.result()
453+
except HttpResponseError as error:
454+
process_storage_error(error)
455+
try:
456+
for _ in range(0, len(done)):
457+
next_chunk = next(dl_tasks)
458+
running_futures.add(asyncio.ensure_future(downloader.process_chunk(next_chunk)))
452459
except StopIteration:
453460
break
454-
else:
455-
running_futures.add(asyncio.ensure_future(downloader.process_chunk(next_chunk)))
456461

457462
if running_futures:
458463
# Wait for the remaining downloads to finish
459-
await asyncio.wait(running_futures)
464+
done, _running_futures = await asyncio.wait(running_futures)
465+
try:
466+
for task in done:
467+
task.result()
468+
except HttpResponseError as error:
469+
process_storage_error(error)
460470
return self.size
461471

462472
async def download_to_stream(self, stream, max_concurrency=1):

0 commit comments

Comments
 (0)