Argo Workflows is widely used for scenarios like scheduled tasks, machine learning, and ETL data processing. However, defining a workflow with YAML can be challenging for those not familiar with Kubernetes. The Hera Python SDK offers a simple and user-friendly alternative to build a workflow with Python code. It supports complex task scenarios, simplifies testing, and integrates seamlessly with the Python ecosystem.
Introduction
Argo Workflows primarily uses YAML to define workflows, which helps keep configurations clear and concise. However, for data scientists unfamiliar with YAML, its strict indentation and hierarchical structure can make designing a complex workflow difficult.
Hera is a Python SDK framework designed to simplify building and submitting Argo workflows. Hera helps you avoid common YAML syntax errors in complex workflows. The Hera Python SDK also offers the following advantages:
-
Simplicity: Hera improves development efficiency with intuitive, easy-to-write code.
-
Easy Python ecosystem integration: Each function is a template that integrates seamlessly with various frameworks in the Python ecosystem. This gives you access to a rich set of Python libraries and tools.
-
Testability: You can directly use Python's testing frameworks, improving code quality and maintainability.
Prerequisites
-
You have installed the Argo components and console and obtained the necessary credentials and Argo Server access IP address. For more information, see Enable batch task orchestration.
-
Hera is installed.
pip install hera-workflows
Scenario 1: Simple DAG diamond
In Argo Workflows, a DAG (directed acyclic graph) is often used to define complex task dependencies. The diamond is a common workflow pattern where a task branches into multiple parallel tasks, which then converge into a single final task. This structure is useful for scenarios that require merging data streams or processing results. This example shows how to use Hera to define a workflow with a diamond structure, where task A runs first, followed by tasks B and C in parallel, which then pass their outputs to task D.
-
Create a file named
simpleDAG.pywith the following content.# Import the required packages. from hera.workflows import DAG, Workflow, script from hera.shared import global_config import urllib3 urllib3.disable_warnings() # Configure the access address and token. global_config.host = "https://${IP}:2746" global_config.token = "abcdefgxxxxxx" # Enter the token you obtained previously. global_config.verify_ssl = "" # The @script decorator is a key feature in Hera that enables near-native Python function orchestration. # It allows you to call the decorated function within a Hera context manager, such as a Workflow or Steps context. # The function still runs normally outside of any Hera context, which means you can write unit tests for it. # This example prints the input message. @script() def echo(message: str): print(message) # Build the workflow. A workflow, a primary resource in Argo and a key class in Hera, # saves templates, sets an entrypoint, and runs them. with Workflow( generate_name="dag-diamond-", entrypoint="diamond", namespace="argo", ) as w: with DAG(name="diamond"): A = echo(name="A", arguments={"message": "A"}) # Build a template. B = echo(name="B", arguments={"message": "B"}) C = echo(name="C", arguments={"message": "C"}) D = echo(name="D", arguments={"message": "D"}) A >> [B, C] >> D # Define dependencies: A runs before B and C, which in turn run before D. # Create the workflow. w.create() -
Run the following command to submit the workflow.
python simpleDAG.py -
After the workflow runs, view the task DAG and results in the Workflow Console (Argo).
The DAG shows that the
dag-diamond-g9v45workflow ran successfully. Node A branches into parallel nodes B and C, which then converge into node D. All nodes have a green checkmark, indicating successful completion.
Scenario 2: MapReduce
You can use a DAG template to implement MapReduce-style data processing in Argo Workflows, which helps organize and coordinate multiple tasks that simulate the Map and Reduce phases. This example builds a simple MapReduce workflow for a numerical processing task with Hera. Each step is a Python function, which allows for easy integration with the Python ecosystem.
-
Configure artifacts. For more information, see Configure Artifacts.
-
Create a file named
map-reduce.pywith the following content. -
Run the following command to submit the workflow.
python map-reduce.py -
After the workflow runs, you can view the task DAG and results in the Workflow Console (Argo).
Related documentation
-
Hera documentation
-
For more details about Hera, see the Hera overview.
-
To learn how to set up and use Hera for LLM training, see Train LLM with Hera.
-
-
YAML deployment examples
-
For the YAML equivalent of the simple-diamond deployment, see dag-diamond.yaml.
-
For the YAML equivalent of the map-reduce deployment, see map-reduce.yaml.
-
Contact us
If you have any product suggestions or questions, you can contact us by joining the DingTalk group (ID: 35688562).