Skip to content

Commit

Permalink
feat: allow setting max_concurrent_downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Jul 23, 2024
1 parent 962f749 commit 9ed2b9b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/stac_asset/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ def cli() -> None:
is_flag=True,
show_default=True,
)
@click.option(
"--max-concurrent-downloads",
help="The maximum number of downloads that can be active at one time",
default=_functions.DEFAULT_MAX_CONCURRENT_DOWNLOADS,
)
# TODO add option to disable content type checking
def download(
href: Optional[str],
Expand All @@ -159,6 +164,7 @@ def download(
keep: bool,
fail_fast: bool,
overwrite: bool,
max_concurrent_downloads: int,
) -> None:
"""Download STAC assets from an item or item collection.
Expand Down Expand Up @@ -197,6 +203,7 @@ def download(
keep=keep,
fail_fast=fail_fast,
overwrite=overwrite,
max_concurrent_downloads=max_concurrent_downloads,
)
)

Expand All @@ -218,6 +225,7 @@ async def download_async(
keep: bool,
fail_fast: bool,
overwrite: bool,
max_concurrent_downloads: int,
) -> None:
config = Config(
alternate_assets=alternate_assets,
Expand Down Expand Up @@ -263,6 +271,7 @@ async def download() -> Union[Item, ItemCollection]:
infer_file_name=False,
config=config,
messages=messages,
max_concurrent_downloads=max_concurrent_downloads,
)

elif type_ == "FeatureCollection":
Expand All @@ -276,6 +285,7 @@ async def download() -> Union[Item, ItemCollection]:
file_name=file_name,
config=config,
messages=messages,
max_concurrent_downloads=max_concurrent_downloads,
)

else:
Expand Down
27 changes: 24 additions & 3 deletions src/stac_asset/_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
from .strategy import ErrorStrategy, FileNameStrategy
from .types import MessageQueue, PathLikeObject

DEFAULT_MAX_CONCURRENT_DOWNLOADS: int = 500
"""The default number of downloads that can be active at once."""


@dataclass
class Download:
Expand Down Expand Up @@ -81,7 +84,7 @@ def __init__(
self,
config: Config,
clients: Optional[List[Client]] = None,
max_concurrent_downloads: int = 500,
max_concurrent_downloads: int = DEFAULT_MAX_CONCURRENT_DOWNLOADS,
) -> None:
config.validate()
self.config = config
Expand Down Expand Up @@ -217,6 +220,7 @@ async def download_item(
messages: Optional[MessageQueue] = None,
clients: Optional[List[Client]] = None,
keep_non_downloaded: bool = False,
max_concurrent_downloads: int = DEFAULT_MAX_CONCURRENT_DOWNLOADS,
) -> Item:
"""Downloads an item to the local filesystem.
Expand All @@ -232,6 +236,8 @@ async def download_item(
clients: Pre-configured clients to use for access
keep_non_downloaded: Keep all assets on the item, even if they're not
downloaded.
max_concurrent_downloads: The maximum number of downloads that can be
active at one time.
Returns:
Item: The `~pystac.Item`, with the updated asset hrefs and self href.
Expand All @@ -245,6 +251,7 @@ async def download_item(
async with Downloads(
config=config or Config(),
clients=clients,
max_concurrent_downloads=max_concurrent_downloads,
) as downloads:
await downloads.add(item, Path(directory), file_name, keep_non_downloaded)
await downloads.download(messages)
Expand All @@ -268,6 +275,7 @@ async def download_collection(
messages: Optional[MessageQueue] = None,
clients: Optional[List[Client]] = None,
keep_non_downloaded: bool = False,
max_concurrent_downloads: int = DEFAULT_MAX_CONCURRENT_DOWNLOADS,
) -> Collection:
"""Downloads a collection to the local filesystem.
Expand All @@ -284,14 +292,20 @@ async def download_collection(
clients: Pre-configured clients to use for access
keep_non_downloaded: Keep all assets on the item, even if they're not
downloaded.
max_concurrent_downloads: The maximum number of downloads that can be
active at one time.
Returns:
Collection: The collection, with updated asset hrefs
Raises:
CantIncludeAndExclude: Raised if both include and exclude are not None.
"""
async with Downloads(config=config or Config(), clients=clients) as downloads:
async with Downloads(
config=config or Config(),
clients=clients,
max_concurrent_downloads=max_concurrent_downloads,
) as downloads:
await downloads.add(collection, Path(directory), file_name, keep_non_downloaded)
await downloads.download(messages)

Expand All @@ -314,6 +328,7 @@ async def download_item_collection(
messages: Optional[MessageQueue] = None,
clients: Optional[List[Client]] = None,
keep_non_downloaded: bool = False,
max_concurrent_downloads: int = DEFAULT_MAX_CONCURRENT_DOWNLOADS,
) -> ItemCollection:
"""Downloads an item collection to the local filesystem.
Expand All @@ -329,6 +344,8 @@ async def download_item_collection(
clients: Pre-configured clients to use for access
keep_non_downloaded: Keep all assets on the item, even if they're not
downloaded.
max_concurrent_downloads: The maximum number of downloads that can be
active at one time.
Returns:
ItemCollection: The item collection, with updated asset hrefs
Expand All @@ -339,7 +356,11 @@ async def download_item_collection(
layout_template = LayoutTemplate(
path_template if path_template is not None else "${id}"
)
async with Downloads(config=config or Config(), clients=clients) as downloads:
async with Downloads(
config=config or Config(),
clients=clients,
max_concurrent_downloads=max_concurrent_downloads,
) as downloads:
for item in item_collection.items:
item.set_self_href(None)
root = Path(directory) / layout_template.substitute(item)
Expand Down
12 changes: 12 additions & 0 deletions src/stac_asset/blocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def download_item(
messages: Optional[MessageQueue] = None,
clients: Optional[List[Client]] = None,
keep_non_downloaded: bool = False,
max_concurrent_downloads: int = _functions.DEFAULT_MAX_CONCURRENT_DOWNLOADS,
) -> Item:
"""Downloads an item to the local filesystem, synchronously.
Expand All @@ -40,6 +41,8 @@ def download_item(
clients: Pre-configured clients to use for access
keep_non_downloaded: Keep all assets on the item, even if they're not
downloaded.
max_concurrent_downloads: The maximum number of downloads that can be
active at one time.
Returns:
Item: The `~pystac.Item`, with the updated asset hrefs and self href.
Expand All @@ -57,6 +60,7 @@ def download_item(
messages=messages,
clients=clients,
keep_non_downloaded=keep_non_downloaded,
max_concurrent_downloads=max_concurrent_downloads,
)
)

Expand All @@ -69,6 +73,7 @@ def download_collection(
messages: Optional[MessageQueue] = None,
clients: Optional[List[Client]] = None,
keep_non_downloaded: bool = False,
max_concurrent_downloads: int = _functions.DEFAULT_MAX_CONCURRENT_DOWNLOADS,
) -> Collection:
"""Downloads a collection to the local filesystem, synchronously.
Expand All @@ -85,6 +90,8 @@ def download_collection(
clients: Pre-configured clients to use for access
keep_non_downloaded: Keep all assets on the item, even if they're not
downloaded.
max_concurrent_downloads: The maximum number of downloads that can be
active at one time.
Returns:
Collection: The collection, with updated asset hrefs
Expand All @@ -101,6 +108,7 @@ def download_collection(
messages=messages,
clients=clients,
keep_non_downloaded=keep_non_downloaded,
max_concurrent_downloads=max_concurrent_downloads,
)
)

Expand All @@ -114,6 +122,7 @@ def download_item_collection(
messages: Optional[MessageQueue] = None,
clients: Optional[List[Client]] = None,
keep_non_downloaded: bool = False,
max_concurrent_downloads: int = _functions.DEFAULT_MAX_CONCURRENT_DOWNLOADS,
) -> ItemCollection:
"""Downloads an item collection to the local filesystem, synchronously.
Expand All @@ -129,6 +138,8 @@ def download_item_collection(
clients: Pre-configured clients to use for access
keep_non_downloaded: Keep all assets on the item, even if they're not
downloaded.
max_concurrent_downloads: The maximum number of downloads that can be
active at one time.
Returns:
ItemCollection: The item collection, with updated asset hrefs
Expand All @@ -146,6 +157,7 @@ def download_item_collection(
clients=clients,
keep_non_downloaded=keep_non_downloaded,
path_template=path_template,
max_concurrent_downloads=max_concurrent_downloads,
)
)

Expand Down

0 comments on commit 9ed2b9b

Please sign in to comment.