Skip to content

Commit 388b81a

Browse files
author
Andrei Neagu
committed
refactor
1 parent aace76a commit 388b81a

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Final
22

3-
_KB: Final[int] = 1024
3+
from pydantic import ByteSize, TypeAdapter
44

5-
DEFAULT_CHUNK_SIZE: Final[int] = 64 * _KB
5+
DEFAULT_CHUNK_SIZE: Final[int] = TypeAdapter(ByteSize).validate_python("1MiB")

packages/service-library/src/servicelib/zip_stream/_zipper.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,19 @@ async def _iter_files(
2727

2828

2929
async def get_zip_archive_stream(
30-
archive_files: ArchiveEntries, *, progress_bar: ProgressBarData | None = None
30+
archive_files: ArchiveEntries,
31+
*,
32+
progress_bar: ProgressBarData | None = None,
33+
chunk_size: int = DEFAULT_CHUNK_SIZE
3134
) -> FileStream:
35+
# NOTE: this is CPU bound task, even though the loop is not blocked,
36+
# the CPU is still used for compressing the content
3237
if progress_bar is None:
3338
progress_bar = ProgressBarData(num_steps=1, description="stream archiver")
39+
40+
# NOTE: do not disable compression or the streams will be
41+
# loaded fully in memory before yielding their content
3442
async for chunk in async_stream_zip(
35-
_iter_files(archive_files, progress_bar), chunk_size=DEFAULT_CHUNK_SIZE
43+
_iter_files(archive_files, progress_bar), chunk_size=chunk_size
3644
):
3745
yield chunk

packages/service-library/tests/test_zip_stream.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from faker import Faker
1111
from pytest_mock import MockerFixture
1212
from pytest_simcore.helpers.comparing import (
13-
assert_same_folder_contents,
14-
get_files_in_folder,
13+
assert_same_contents,
14+
get_files_info_from_path,
1515
get_relative_to,
1616
)
1717
from servicelib.archiving_utils import unarchive_dir
@@ -121,7 +121,7 @@ async def test_get_zip_archive_stream(
121121
await unarchive_dir(local_archive_path, local_unpacked_archive)
122122

123123
# 3. compare files in directories (same paths & sizes)
124-
await assert_same_folder_contents(
125-
get_files_in_folder(local_files_dir),
126-
get_files_in_folder(local_unpacked_archive),
124+
await assert_same_contents(
125+
get_files_info_from_path(local_files_dir),
126+
get_files_info_from_path(local_unpacked_archive),
127127
)

0 commit comments

Comments
 (0)