Build large-scale Argo Workflows with the Python SDK
Argo Workflows is widely used for scenarios such as scheduled tasks, machine learning, and ETL data processing. However, defining a workflow in YAML can be challenging for users who are not proficient with Kubernetes. The Hera Python SDK offers a simple, user-friendly alternative for building a workflow with Python code. It supports complex task scenarios, is easy to test, and integrates seamlessly with the Python ecosystem.
Introduction
Argo Workflows primarily relies on YAML to define a workflow, which ensures clear and concise configurations. However, for data scientists unfamiliar with YAML, its strict indentation requirements and hierarchical structure can make configuring complex workflows difficult.
Hera is a Python SDK designed to simplify building and submitting Argo Workflows. When handling a complex workflow, Hera helps you avoid potential syntax errors that can occur in YAML. Hera also provides the following benefits:
-
Hera improves development efficiency with code that is easy to understand and write.
-
Each function acts as a template that seamlessly integrates with various frameworks in the Python ecosystem, providing access to a rich set of Python libraries and tools.
-
You can use Python's testing frameworks directly, improving code quality and maintainability.
Prerequisites
-
You have installed the Argo components and console, and obtained the access credentials and Argo Server IP address. For more information, see Enable batch task orchestration.
-
You have installed Hera.
pip install hera-workflows
Scenario 1: Simple DAG Diamond
In Argo Workflows, a DAG is often used to define complex task dependencies. The diamond structure is a common workflow pattern where multiple tasks run in parallel, and their results are then aggregated into a single, subsequent task. This structure is suitable for use cases that require merging different data streams or processing results. This example uses Hera to define a diamond-structured workflow where an initial task (A) is followed by two parallel tasks (B and C), which then converge into a final 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 earlier. global_config.verify_ssl = "" # The script decorator function is a key Hera feature that enables near-native Python function orchestration. # It allows you to call the function within a Hera context manager, such as a Workflow or Steps context. # The function also 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 is the primary resource in Argo and a key class in Hera, responsible for saving a template, setting an entrypoint, and running the template. with Workflow( generate_name="dag-diamond-", entrypoint="diamond", namespace="argo", ) as w: with DAG(name="diamond"): A = echo(name="A", arguments={"message": "A"}) # Build the 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 task dependencies: Tasks B and C depend on A, and D depends on B and C. # Create the workflow. w.create() -
Run the following command to submit the workflow.
python simpleDAG.py -
After the workflow runs, view the DAG and results in the Workflow Console (Argo).
The DAG for the dag-diamond-g9v45 workflow displays a diamond shape: the main node connects to node A, which branches to nodes B and C. Nodes B and C then converge into the bottom node D. All nodes show a green check mark, indicating successful execution.
Scenario 2: MapReduce
To implement MapReduce-style data processing in Argo Workflows, you can use its DAG template to organize and coordinate multiple tasks, simulating the Map and Reduce phases. The following example shows how to use Hera to build a simple MapReduce workflow for a word count task in text files. Because each step is a Python function, integration with the Python ecosystem is straightforward.
-
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 workflow's DAG and results in the Workflow Console (Argo). On the WORKFLOW DETAILS page of the Argo Workflows console, you can see that all nodes of the map-reduce workflow (split → 4 parallel map tasks → reduce) have executed successfully. Each node displays a green check mark.
References
-
Hera documentation
-
For more information about Hera, see Hera overview.
-
To learn how to set up and use Hera for LLM training, see Train LLM with Hera.
-
-
Sample YAML deployments
-
To learn how to deploy a simple-diamond workflow using YAML, see dag-diamond.yaml.
-
To learn how to deploy a map-reduce workflow using YAML, 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).