diff --git a/changelog/+default-max-concurrent-execution.added.md b/changelog/+default-max-concurrent-execution.added.md new file mode 100644 index 000000000..31703244f --- /dev/null +++ b/changelog/+default-max-concurrent-execution.added.md @@ -0,0 +1 @@ +The default number of batch tasks that run at the same time, 5, is now available as `DEFAULT_MAX_CONCURRENT_EXECUTION` in `infrahub_sdk.constants`. `InfrahubBatch`, `InfrahubBatchSync` and `Config` use it as their default, so code that receives an optional concurrency value can pass `DEFAULT_MAX_CONCURRENT_EXECUTION` when the value is `None` instead of repeating the number 5. diff --git a/infrahub_sdk/batch.py b/infrahub_sdk/batch.py index 6e9cc1cb0..8ecbfdaec 100644 --- a/infrahub_sdk/batch.py +++ b/infrahub_sdk/batch.py @@ -6,6 +6,8 @@ from dataclasses import dataclass from typing import TYPE_CHECKING, Any +from .constants import DEFAULT_MAX_CONCURRENT_EXECUTION + if TYPE_CHECKING: from .node import InfrahubNode, InfrahubNodeSync @@ -56,7 +58,7 @@ class InfrahubBatch: def __init__( self, semaphore: asyncio.Semaphore | None = None, - max_concurrent_execution: int = 5, + max_concurrent_execution: int = DEFAULT_MAX_CONCURRENT_EXECUTION, return_exceptions: bool = False, ) -> None: self._tasks: list[BatchTask] = [] @@ -88,7 +90,9 @@ async def execute(self) -> AsyncGenerator: class InfrahubBatchSync: - def __init__(self, max_concurrent_execution: int = 5, return_exceptions: bool = False) -> None: + def __init__( + self, max_concurrent_execution: int = DEFAULT_MAX_CONCURRENT_EXECUTION, return_exceptions: bool = False + ) -> None: self._tasks: list[BatchTaskSync] = [] self.max_concurrent_execution = max_concurrent_execution self.return_exceptions = return_exceptions diff --git a/infrahub_sdk/config.py b/infrahub_sdk/config.py index 652ee63f7..7d23d75e3 100644 --- a/infrahub_sdk/config.py +++ b/infrahub_sdk/config.py @@ -9,7 +9,7 @@ from pydantic_settings import BaseSettings, InitSettingsSource, PydanticBaseSettingsSource, SettingsConfigDict from typing_extensions import Self -from .constants import InfrahubClientMode, Priority +from .constants import DEFAULT_MAX_CONCURRENT_EXECUTION, InfrahubClientMode, Priority from .playback import JSONPlayback from .recorder import JSONRecorder, NoRecorder, Recorder, RecorderType from .types import AsyncRequester, InfrahubLoggers, RequesterTransport, SyncRequester @@ -54,7 +54,9 @@ class ConfigBase(BaseSettings): ) identifier: str | None = Field(default=None, description="Tracker identifier") insert_tracker: bool = Field(default=False, description="Insert a tracker on queries to the server") - max_concurrent_execution: int = Field(default=5, description="Max concurrent execution in batch mode") + max_concurrent_execution: int = Field( + default=DEFAULT_MAX_CONCURRENT_EXECUTION, description="Max concurrent execution in batch mode" + ) mode: InfrahubClientMode = Field(default=InfrahubClientMode.DEFAULT, description="Default mode for the client") pagination_size: int = Field(default=50, description="Page size for queries to the server") priority: Priority | None = Field( diff --git a/infrahub_sdk/constants.py b/infrahub_sdk/constants.py index 03e6302ac..1cd5e5a47 100644 --- a/infrahub_sdk/constants.py +++ b/infrahub_sdk/constants.py @@ -1,7 +1,9 @@ -"""Enumerations shared across the Infrahub SDK.""" +"""Enumerations and constants shared across the Infrahub SDK.""" import enum +DEFAULT_MAX_CONCURRENT_EXECUTION = 5 + class InfrahubClientMode(str, enum.Enum): DEFAULT = "default"