How to Use << Operator in Airflow for Task Dependencies
In Airflow, the
<< operator is used to set upstream dependencies between tasks, meaning the task on the left will run after the task on the right. It is a shorthand for set_upstream() and helps write cleaner DAG code.Syntax
The << operator in Airflow is used between two task objects to set the left task as downstream of the right task. It means the left task will wait for the right task to complete before running.
Syntax parts:
task1 << task2: task1 runs after task2 finishes.- This is equivalent to
task1.set_upstream(task2).
python
task1 << task2
Example
This example shows how to use the << operator to set task dependencies in an Airflow DAG. Task task2 will run before task1.
python
from airflow import DAG from airflow.operators.dummy_operator import DummyOperator from datetime import datetime default_args = { 'start_date': datetime(2024, 1, 1), } dag = DAG('example_dag', default_args=default_args, schedule_interval='@daily') task1 = DummyOperator(task_id='task1', dag=dag) task2 = DummyOperator(task_id='task2', dag=dag) # Set task1 to run after task2 using << operator task1 << task2
Output
DAG 'example_dag' created with task2 upstream of task1
Common Pitfalls
Common mistakes when using the << operator include:
- Confusing the direction:
task1 << task2means task1 runs after task2, not the other way around. - Using it with non-task objects causes errors.
- Mixing
<<and>>operators inconsistently can make DAGs hard to read.
Always double-check task order to avoid unexpected execution sequences.
python
## Wrong usage example: # task2 << task1 # This makes task2 run after task1, opposite of intended ## Correct usage: task1 << task2 # task1 runs after task2
Quick Reference
| Operator | Meaning | Equivalent Method |
|---|---|---|
| task1 << task2 | task1 runs after task2 | task1.set_upstream(task2) |
| task1 >> task2 | task1 runs before task2 | task1.set_downstream(task2) |
Key Takeaways
The << operator sets the left task to run after the right task in Airflow.
It is a shorthand for the set_upstream() method.
Be careful with the direction to avoid reversing task order.
Use << only between Airflow task objects.
Consistent use of << and >> improves DAG readability.