This repository was archived by the owner on Mar 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Feat: Sync spawn methods + fixing sync_result
#321
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6b7a209
feat: add sync spawn method
mrkaye97 27e5f7a
feat: example
mrkaye97 152f31e
fix: sync workflow run + sync_result
mrkaye97 0bd6e56
fix: examples
mrkaye97 9b970f1
fix: lint
mrkaye97 28f085c
feat: bulk spawn
mrkaye97 209559a
chore: ver
mrkaye97 17a05f5
fix: test
mrkaye97 4911afa
fix: lint
mrkaye97 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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import pytest | ||
|
|
||
| from hatchet_sdk import Hatchet, Worker | ||
|
|
||
|
|
||
| # requires scope module or higher for shared event loop | ||
| @pytest.mark.parametrize("worker", ["fanout_sync"], indirect=True) | ||
| def test_run(hatchet: Hatchet, worker: Worker) -> None: | ||
| run = hatchet.admin.run_workflow("SyncFanoutParent", {"n": 2}) | ||
| result = run.sync_result() | ||
| assert len(result["spawn"]["results"]) == 2 |
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,20 @@ | ||
| import asyncio | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| from hatchet_sdk import new_client | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| load_dotenv() | ||
| hatchet = new_client() | ||
|
|
||
| hatchet.admin.run_workflow( | ||
| "SyncFanoutParent", | ||
| {"test": "test"}, | ||
| options={"additional_metadata": {"hello": "moon"}}, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
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,55 @@ | ||
| from typing import Any | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| from hatchet_sdk import Context, Hatchet | ||
| from hatchet_sdk.workflow_run import WorkflowRunRef | ||
|
|
||
| load_dotenv() | ||
|
|
||
| hatchet = Hatchet(debug=True) | ||
|
|
||
|
|
||
| @hatchet.workflow(on_events=["parent:create"]) | ||
| class SyncFanoutParent: | ||
| @hatchet.step(timeout="5m") | ||
| def spawn(self, context: Context) -> dict[str, Any]: | ||
| print("spawning child") | ||
|
|
||
| n = context.workflow_input().get("n", 5) | ||
|
|
||
| runs = context.spawn_workflows( | ||
| [ | ||
| { | ||
| "workflow_name": "SyncFanoutChild", | ||
| "input": {"a": str(i)}, | ||
| "key": f"child{i}", | ||
| "options": {"additional_metadata": {"hello": "earth"}}, | ||
| } | ||
| for i in range(n) | ||
| ] | ||
| ) | ||
|
|
||
| results = [r.sync_result() for r in runs] | ||
|
|
||
| print(f"results {results}") | ||
|
|
||
| return {"results": results} | ||
|
|
||
|
|
||
| @hatchet.workflow(on_events=["child:create"]) | ||
| class SyncFanoutChild: | ||
| @hatchet.step() | ||
| def process(self, context: Context) -> dict[str, str]: | ||
| return {"status": "success " + context.workflow_input()["a"]} | ||
|
|
||
|
|
||
| def main() -> None: | ||
| worker = hatchet.worker("sync-fanout-worker", max_runs=40) | ||
| worker.register_workflow(SyncFanoutParent()) | ||
| worker.register_workflow(SyncFanoutChild()) | ||
| worker.start() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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
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
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'm not 100% sure this is safe, but I think the only risk of it causing issues is if there's no event loop, which I can't imagine being super common. It's a little tough to test well though