Parallel¶
Internal implementation module: async_durable_execution._operation.parallel.
Use parallel() for explicit durable branches and configurable batch
completion behavior.
Concurrent executor for parallel and map operations.
Classes¶
CompletionReason
¶
Bases: Enum
Why a map() or parallel() operation stopped collecting results.
Values
ALL_COMPLETED: Every item or branch reached a terminal state and no
earlier success, failure, or custom completion condition applied.
MIN_SUCCESSFUL_REACHED: The configured min_successful threshold was
reached.
FAILURE_TOLERANCE_EXCEEDED: The number of failures exceeded the
configured tolerated_failure_count, or no failure tolerance was
configured and at least one failure was observed.
CUSTOM_COMPLETION_SUCCEEDED: A custom completion function completed the
operation successfully.
CUSTOM_COMPLETION_FAILED: A custom completion function completed the
operation as failed.
CompletionStatus
dataclass
¶
Live completion progress passed to a custom completion function.
Attributes:
| Name | Type | Description |
|---|---|---|
success_count |
int
|
Number of items or branches that have completed successfully. |
failure_count |
int
|
Number of items or branches that have failed. |
total_count |
int
|
Total number of items or branches registered for the operation. |
completed_count |
int
|
Calculated number of terminal items or branches,
equal to |
all_completed |
bool
|
Whether every registered item or branch is terminal. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any count is negative, or if completed count exceeds
|
CompletionDecision
dataclass
¶
CompletionDecision(
should_complete: bool,
completion_reason: CompletionReason | None = None,
)
Decision returned by a completion condition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
should_complete
|
bool
|
Whether the operation should stop collecting results. |
required |
completion_reason
|
CompletionReason | None
|
Required when |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Attributes¶
is_succeeded
property
¶
Whether this decision completes the operation successfully.
Methods:¶
complete
staticmethod
¶
complete(
completion_reason: CompletionReason,
) -> CompletionDecision
Create a decision that completes the operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
completion_reason
|
CompletionReason
|
Reason to store on the resulting |
required |
Returns:
| Type | Description |
|---|---|
CompletionDecision
|
A |
continue_execution
staticmethod
¶
continue_execution() -> CompletionDecision
Create a decision that keeps collecting item or branch results.
Returns:
| Type | Description |
|---|---|
CompletionDecision
|
A |
NestingType
¶
Bases: Enum
Control how child contexts are created for batch operations.
CompletionConfig
dataclass
¶
CompletionConfig(
min_successful: int | None = None,
tolerated_failure_count: int | None = None,
should_complete: ShouldComplete | None = None,
)
Configuration for determining when parallel/map operations complete.
Without should_complete, completion is evaluated in this order:
- Complete successfully when
success_count >= min_successful, ifmin_successfulis configured. - Complete as failed when
failure_count > tolerated_failure_count, iftolerated_failure_countis configured. - Complete as failed when
tolerated_failure_countisNoneand at least one failure is observed. - Complete successfully when every item or branch has completed.
If should_complete is configured, it fully controls the completion
decision and must return a CompletionDecision.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_successful
|
int | None
|
Optional success threshold. Reaching this count completes the operation successfully. |
None
|
tolerated_failure_count
|
int | None
|
Optional failure tolerance. Failures complete
the operation as failed only after they exceed this count. When this
is |
None
|
should_complete
|
ShouldComplete | None
|
Optional custom completion function. This is mutually
exclusive with |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Attributes¶
has_custom_should_complete
property
¶
Whether a custom completion function is configured.
Methods:¶
thresholds
classmethod
¶
thresholds(
*,
min_successful: int | None = None,
tolerated_failure_count: int | None = None,
) -> CompletionConfig
Create a threshold-based completion configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_successful
|
int | None
|
Optional success threshold. The operation completes successfully once this many items or branches succeed. |
None
|
tolerated_failure_count
|
int | None
|
Optional failure tolerance. The operation completes as failed once failures exceed this count. |
None
|
Returns:
| Type | Description |
|---|---|
CompletionConfig
|
A |
first_successful
classmethod
¶
first_successful() -> CompletionConfig
Create a configuration that completes after the first success.
Returns:
| Type | Description |
|---|---|
CompletionConfig
|
A |
all_completed
classmethod
¶
all_completed() -> CompletionConfig
Create a configuration with no explicit thresholds.
Returns:
| Type | Description |
|---|---|
CompletionConfig
|
A |
all_successful
classmethod
¶
all_successful() -> CompletionConfig
Create a configuration that requires every item or branch to succeed.
Returns:
| Type | Description |
|---|---|
CompletionConfig
|
A |
custom
classmethod
¶
custom(should_complete: ShouldComplete) -> CompletionConfig
Create a configuration that delegates completion to a callback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
should_complete
|
ShouldComplete
|
Deterministic callable that receives a
|
required |
Returns:
| Type | Description |
|---|---|
CompletionConfig
|
A |
completion_decision
¶
completion_decision(
status: CompletionStatus,
) -> CompletionDecision
Evaluate whether the supplied progress status should complete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
CompletionStatus
|
Current completion progress for a |
required |
Returns:
| Type | Description |
|---|---|
CompletionDecision
|
A |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a custom completion callback returns |
BatchItemStatus
¶
Bases: Enum
Status of one item or branch inside a batch-style operation.
A CANCELLED item started but did not finish before the parent reached
an early completion condition. Its cancellation is stored in the parent
BatchResult rather than checkpointed as a child result.
BatchItem
dataclass
¶
BatchItem(
index: int,
status: BatchItemStatus,
result: R | None = None,
error: ErrorObject | None = None,
)
Bases: MappingModel, Generic[R]
Result record for one branch or iteration in BatchResult.
BatchResult
dataclass
¶
BatchResult(
all: list[BatchItem[R]],
completion_reason: CompletionReason,
)
Bases: MappingModel, Generic[R]
Aggregated outcome of a map() or parallel() operation.
Functions:¶
parallel
¶
parallel(
branches: Iterable[Callable[[], Awaitable[T]]],
*,
name: str | None = None,
max_concurrency: int | None = None,
completion_config: CompletionConfig | None = None,
serdes: SerDes | None = None,
item_serdes: SerDes | None = None,
summary_generator: SummaryGenerator
| None = ParallelSummaryGenerator(),
nesting_type: NestingType = NESTED,
) -> Task[BatchResult[T]]
Start a durable parallel operation.
Each branch is an async zero-argument callable, typically a bound durable
callable such as fetch_user(user_id). Branches run in child durable
contexts and may contain durable operations such as step() or wait().
The returned object is an asyncio.Task; awaiting it yields a BatchResult.
Calling parallel() without immediately awaiting it schedules the durable
operation in the background, consistent with other operation helpers.
By default, parallel() uses CompletionConfig.all_successful(): every
branch must succeed, and the first failure completes the operation as failed.
Pass completion_config to use threshold-based or custom completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
branches
|
Iterable[Callable[[], Awaitable[T]]]
|
Async zero-argument branch callables to run concurrently. |
required |
name
|
str | None
|
Optional durable operation name. |
None
|
max_concurrency
|
int | None
|
Optional limit for in-flight branches. A suspended branch retains its slot until it reaches a terminal state. |
None
|
completion_config
|
CompletionConfig | None
|
Optional completion policy. Use
|
None
|
serdes
|
SerDes | None
|
Optional serializer for the final |
None
|
item_serdes
|
SerDes | None
|
Optional serializer for each branch result. |
None
|
summary_generator
|
SummaryGenerator | None
|
Optional callable used to summarize oversized checkpoint payloads. |
ParallelSummaryGenerator()
|
nesting_type
|
NestingType
|
Whether branch operations use nested or flat operation identifiers. |
NESTED
|
Returns:
| Type | Description |
|---|---|
Task[BatchResult[T]]
|
An |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If |
RuntimeError
|
If called outside a durable context. |