-
Notifications
You must be signed in to change notification settings - Fork 37
[WIP] Add prefect manager support #788
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
ptigas
wants to merge
6
commits into
materialsproject:main
Choose a base branch
from
diffractivelabs:ptigas/prefect_support
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 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e5940a
added prefect support
ptigas 61769ba
set TaskRunner instead
ptigas 84bbb3b
addressed some pr comments and added dynamic flows support
ptigas 79b4f11
Merge branch 'main' into ptigas/prefect_support
ptigas b7fd673
fixed some bugs in prefect.py
ptigas fae7776
Merge branch 'main' into ptigas/prefect_support
ptigas 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,207 @@ | ||
| """ | ||
| Example demonstrating how to use jobflow with Prefect. | ||
|
|
||
| This example shows how to: | ||
| 1. Create jobflow Jobs and Flows | ||
| 2. Convert them to Prefect workflows | ||
| 3. Run them locally using Prefect | ||
| 4. Create deployments for a Prefect cluster | ||
| """ | ||
|
|
||
| import jobflow | ||
| from jobflow import job, Job, Flow | ||
| from jobflow.managers.prefect import PrefectManager, run_on_prefect, flow_to_prefect_flow | ||
|
|
||
|
|
||
| # Define some simple jobs | ||
| @job | ||
| def add(a: int, b: int) -> int: | ||
| """Add two numbers.""" | ||
| print(f"Adding {a} + {b}") | ||
| return a + b | ||
|
|
||
|
|
||
| @job | ||
| def multiply(a: int, b: int) -> int: | ||
| """Multiply two numbers.""" | ||
| print(f"Multiplying {a} * {b}") | ||
| return a * b | ||
|
|
||
|
|
||
| @job | ||
| def subtract(a: int, b: int) -> int: | ||
| """Subtract two numbers.""" | ||
| print(f"Subtracting {a} - {b}") | ||
| return a - b | ||
|
|
||
|
|
||
| def example_single_job(): | ||
| """Example of running a single job with Prefect.""" | ||
| print("\n=== Single Job Example ===") | ||
|
|
||
| # Create a simple job | ||
| job1 = add(5, 3) | ||
|
|
||
| try: | ||
| # Run using Prefect | ||
| result = run_on_prefect(job1, flow_name="single_add_job") | ||
| print(f"Result: {result}") | ||
| except ImportError: | ||
| print("Prefect is not installed. Please install with: pip install prefect") | ||
| except Exception as e: | ||
| print(f"Error running job: {e}") | ||
|
|
||
|
|
||
| def example_simple_flow(): | ||
| """Example of running a simple flow with Prefect.""" | ||
| print("\n=== Simple Flow Example ===") | ||
|
|
||
| # Create jobs | ||
| job1 = add(5, 3) | ||
| job2 = multiply(job1.output, 2) | ||
| job3 = subtract(job2.output, 1) | ||
|
|
||
| # Create flow | ||
| flow = Flow([job1, job2, job3], output=job3.output, name="arithmetic_flow") | ||
|
|
||
| try: | ||
| # Run using Prefect | ||
| result = run_on_prefect(flow, flow_name="arithmetic_workflow") | ||
| print(f"Flow result: {result}") | ||
| except ImportError: | ||
| print("Prefect is not installed. Please install with: pip install prefect") | ||
| except Exception as e: | ||
| print(f"Error running flow: {e}") | ||
|
|
||
|
|
||
| def example_parallel_flow(): | ||
| """Example of running a parallel flow with Prefect.""" | ||
| print("\n=== Parallel Flow Example ===") | ||
|
|
||
| # Create independent jobs that can run in parallel | ||
| job1 = add(1, 2) | ||
| job2 = add(3, 4) | ||
| job3 = add(5, 6) | ||
|
|
||
| # These jobs depend on the parallel jobs | ||
| job4 = multiply(job1.output, job2.output) | ||
| job5 = multiply(job3.output, 2) | ||
|
|
||
| # Final job combines everything | ||
| job6 = add(job4.output, job5.output) | ||
|
|
||
| # Create flow | ||
| flow = Flow( | ||
| [job1, job2, job3, job4, job5, job6], | ||
| output=job6.output, | ||
| name="parallel_arithmetic_flow" | ||
| ) | ||
|
|
||
| try: | ||
| # Run using Prefect with concurrent task runner | ||
| result = run_on_prefect( | ||
| flow, | ||
| flow_name="parallel_workflow", | ||
| task_runner="concurrent" | ||
| ) | ||
| print(f"Parallel flow result: {result}") | ||
| except ImportError: | ||
| print("Prefect is not installed. Please install with: pip install prefect") | ||
| except Exception as e: | ||
| print(f"Error running parallel flow: {e}") | ||
|
|
||
|
|
||
| def example_prefect_manager(): | ||
| """Example using the PrefectManager class.""" | ||
| print("\n=== PrefectManager Example ===") | ||
|
|
||
| try: | ||
| # Create manager | ||
| manager = PrefectManager(task_runner="sequential") | ||
|
|
||
| # Create a flow | ||
| job1 = add(10, 5) | ||
| job2 = multiply(job1.output, 3) | ||
| flow = Flow([job1, job2], output=job2.output, name="manager_test_flow") | ||
|
|
||
| # Submit flow (this would be async in real usage) | ||
| print("Creating Prefect flow...") | ||
| prefect_flow = flow_to_prefect_flow(flow, flow_name="manager_workflow") | ||
| print(f"Created Prefect flow: {prefect_flow}") | ||
|
|
||
| # You could also create a deployment like this: | ||
| # deployment = manager.create_deployment( | ||
| # flow, | ||
| # deployment_name="my_deployment", | ||
| # work_pool_name="my_work_pool" | ||
| # ) | ||
| # print(f"Created deployment: {deployment}") | ||
|
|
||
| except ImportError: | ||
| print("Prefect is not installed. Please install with: pip install prefect") | ||
| except Exception as e: | ||
| print(f"Error with PrefectManager: {e}") | ||
|
|
||
|
|
||
| def example_error_handling(): | ||
| """Example showing error handling in Prefect workflows.""" | ||
| print("\n=== Error Handling Example ===") | ||
|
|
||
| @job | ||
| def divide(a: int, b: int) -> float: | ||
| """Divide two numbers.""" | ||
| if b == 0: | ||
| raise ValueError("Cannot divide by zero!") | ||
| return a / b | ||
|
|
||
| # First, show a successful division | ||
| working_job = divide(10, 2) | ||
| try: | ||
| result = run_on_prefect(working_job, flow_name="working_division") | ||
| print(f"Successful division result: {list(result.values())[0]}") | ||
| except Exception as e: | ||
| print(f"Error in working job: {e}") | ||
| return | ||
|
|
||
| # Now show error handling (this will generate Prefect error logs, but that's expected) | ||
| print("Testing error handling (expect to see error logs below)...") | ||
| failing_job = divide(10, 0) | ||
|
|
||
| try: | ||
| result = run_on_prefect(failing_job, flow_name="failing_job") | ||
| print(f"❌ Unexpected success: {result}") | ||
| except ImportError: | ||
| print("Prefect is not installed. Please install with: pip install prefect") | ||
| except Exception as e: | ||
| print(f"✅ Expected error caught and handled: {type(e).__name__}: {e}") | ||
| print(" (The error traceback above is expected - Prefect logs all failures)") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print("Jobflow + Prefect Integration Examples") | ||
| print("=====================================") | ||
| print("NOTE: Error tracebacks in logs are expected for the error handling example.") | ||
| print(" The examples demonstrate both successful execution and error handling.") | ||
| print() | ||
|
|
||
| # Check if Prefect is available | ||
| try: | ||
| import prefect | ||
| print(f"Prefect version: {prefect.__version__}") | ||
| except ImportError: | ||
| print("Prefect is not installed. Some examples will not work.") | ||
| print("Install with: pip install prefect") | ||
|
|
||
| # Run examples | ||
| example_single_job() | ||
| example_simple_flow() | ||
| example_parallel_flow() | ||
| example_prefect_manager() | ||
| example_error_handling() | ||
|
|
||
| print("\n=== Examples Complete ===") | ||
| print("To deploy to a Prefect cluster:") | ||
| print("1. Start Prefect server: prefect server start") | ||
| print("2. Create work pool: prefect work-pool create --type process my_pool") | ||
| print("3. Create deployment using PrefectManager.create_deployment()") | ||
| print("4. Run worker: prefect worker start --pool my_pool") |
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 think this is better suited for documentation rather than as a module within the codebase.