-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparallel_flat.py
More file actions
27 lines (22 loc) · 1.13 KB
/
parallel_flat.py
File metadata and controls
27 lines (22 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""Example demonstrating parallel operations for concurrent execution."""
from typing import Any
from aws_durable_execution_sdk_python.config import ParallelConfig, NestingType
from aws_durable_execution_sdk_python.context import DurableContext
from aws_durable_execution_sdk_python.execution import durable_execution
from aws_durable_execution_sdk_python.config import Duration
@durable_execution
def handler(_event: Any, context: DurableContext) -> list[str]:
"""Execute multiple operations in parallel using context.parallel()."""
# Use context.parallel() to execute functions concurrently and extract results immediately
return context.parallel(
functions=[
lambda ctx: ctx.step(lambda _: "task 1 completed", name="task1"),
lambda ctx: ctx.step(lambda _: "task 2 completed", name="task2"),
lambda ctx: (
ctx.wait(Duration.from_seconds(1), name="wait_in_task3"),
"task 3 completed after wait",
)[1],
],
name="parallel_operation",
config=ParallelConfig(max_concurrency=2, nesting_type=NestingType.FLAT),
).get_results()