-
Notifications
You must be signed in to change notification settings - Fork 17
feat: parallel and map branch name #387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wangyb-A
wants to merge
2
commits into
main
Choose a base branch
from
feat/item_namer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,4 +31,6 @@ dist/ | |
| .kiro/ | ||
|
|
||
| /examples/build/* | ||
| /examples/*.zip | ||
| /examples/*.zip | ||
|
|
||
| .env | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| """Example demonstrating map operations with custom iteration naming.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from aws_durable_execution_sdk_python.config import MapConfig | ||
| from aws_durable_execution_sdk_python.context import DurableContext | ||
| from aws_durable_execution_sdk_python.execution import durable_execution | ||
|
|
||
|
|
||
| @durable_execution | ||
| def handler(_event: Any, context: DurableContext) -> list[str]: | ||
| """Process orders using context.map() with custom iteration names.""" | ||
| orders = [ | ||
| {"id": "order-101", "amount": 25}, | ||
| {"id": "order-102", "amount": 50}, | ||
| {"id": "order-103", "amount": 75}, | ||
| ] | ||
|
|
||
| return context.map( | ||
| inputs=orders, | ||
| func=lambda ctx, order, index, _: ctx.step( | ||
| lambda _: f"processed-{order['id']}-${order['amount']}", | ||
| name=f"process_{order['id']}", | ||
| ), | ||
| name="process_orders", | ||
| config=MapConfig( | ||
| max_concurrency=2, | ||
| item_namer=lambda order, index: f"order-{order['id']}", | ||
| ), | ||
| ).get_results() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| """Example demonstrating parallel operations with named branches.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from aws_durable_execution_sdk_python.config import ParallelBranch, ParallelConfig | ||
| from aws_durable_execution_sdk_python.context import DurableContext | ||
| from aws_durable_execution_sdk_python.execution import durable_execution | ||
|
|
||
|
|
||
| @durable_execution | ||
| def handler(_event: Any, context: DurableContext) -> list[str]: | ||
| """Execute named parallel branches using ParallelBranch.""" | ||
|
|
||
| return context.parallel( | ||
| functions=[ | ||
| ParallelBranch( | ||
| func=lambda ctx: ctx.step( | ||
| lambda _: "user-data-loaded", name="load_user" | ||
| ), | ||
| name="fetch-user-data", | ||
| ), | ||
| ParallelBranch( | ||
| func=lambda ctx: ctx.step( | ||
| lambda _: "orders-loaded", name="load_orders" | ||
| ), | ||
| name="fetch-order-history", | ||
| ), | ||
| ParallelBranch( | ||
| func=lambda ctx: ctx.step(lambda _: "prefs-loaded", name="load_prefs"), | ||
| name="fetch-preferences", | ||
| ), | ||
| ], | ||
| name="load_all_data", | ||
| config=ParallelConfig(max_concurrency=3), | ||
| ).get_results() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| """Tests for map_with_item_namer example.""" | ||
|
|
||
| import pytest | ||
| from src.map import map_with_item_namer | ||
| from test.conftest import deserialize_operation_payload | ||
|
|
||
| from aws_durable_execution_sdk_python.execution import InvocationStatus | ||
| from aws_durable_execution_sdk_python.lambda_service import ( | ||
| OperationStatus, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.example | ||
| @pytest.mark.durable_execution( | ||
| handler=map_with_item_namer.handler, | ||
| lambda_function_name="map with item namer", | ||
| ) | ||
| def test_map_with_item_namer(durable_runner): | ||
| """Test map example with custom item_namer for iteration naming.""" | ||
| with durable_runner: | ||
| result = durable_runner.run(input="test", timeout=10) | ||
|
|
||
| assert result.status is InvocationStatus.SUCCEEDED | ||
| assert deserialize_operation_payload(result.result) == [ | ||
| "processed-order-101-$25", | ||
| "processed-order-102-$50", | ||
| "processed-order-103-$75", | ||
| ] | ||
|
|
||
| # Get the map operation | ||
| map_op = result.get_context("process_orders") | ||
| assert map_op is not None | ||
| assert map_op.status is OperationStatus.SUCCEEDED | ||
|
|
||
| # Verify custom iteration names from item_namer | ||
| assert len(map_op.child_operations) == 3 | ||
| child_names = {op.name for op in map_op.child_operations} | ||
| expected_names = {"order-order-101", "order-order-102", "order-order-103"} | ||
| assert child_names == expected_names |
45 changes: 45 additions & 0 deletions
45
examples/test/parallel/test_parallel_with_named_branches.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """Tests for parallel_with_named_branches example.""" | ||
|
|
||
| import pytest | ||
| from src.parallel import parallel_with_named_branches | ||
| from test.conftest import deserialize_operation_payload | ||
|
|
||
| from aws_durable_execution_sdk_python.execution import InvocationStatus | ||
| from aws_durable_execution_sdk_python.lambda_service import ( | ||
| OperationStatus, | ||
| OperationType, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.example | ||
| @pytest.mark.durable_execution( | ||
| handler=parallel_with_named_branches.handler, | ||
| lambda_function_name="parallel with named branches", | ||
| ) | ||
| def test_parallel_with_named_branches(durable_runner): | ||
| """Test parallel example with named branches using ParallelBranch.""" | ||
| with durable_runner: | ||
| result = durable_runner.run(input="test", timeout=10) | ||
|
|
||
| assert result.status is InvocationStatus.SUCCEEDED | ||
| assert deserialize_operation_payload(result.result) == [ | ||
| "user-data-loaded", | ||
| "orders-loaded", | ||
| "prefs-loaded", | ||
| ] | ||
|
|
||
| # Get the parallel operation | ||
| parallel_op = result.get_context("load_all_data") | ||
| assert parallel_op is not None | ||
| assert parallel_op.status is OperationStatus.SUCCEEDED | ||
|
|
||
| # Verify custom branch names from ParallelBranch | ||
| assert len(parallel_op.child_operations) == 3 | ||
| child_names = {op.name for op in parallel_op.child_operations} | ||
| expected_names = {"fetch-user-data", "fetch-order-history", "fetch-preferences"} | ||
| assert child_names == expected_names | ||
|
|
||
| # Verify all children succeeded | ||
| for child in parallel_op.child_operations: | ||
| assert child.operation_type == OperationType.CONTEXT | ||
| assert child.status is OperationStatus.SUCCEEDED |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather provide this functionality as a decorator, e.g.
@durable_parallel_branch(name=...). This would be in align with the other decorators we have now.