0
0
Apache Airflowdevops~20 mins

Dynamic task generation with loops in Apache Airflow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Task Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of dynamically generated Airflow tasks with loop
Given the following Airflow DAG snippet, what will be the output when the DAG is parsed and tasks are listed?
Apache Airflow
from airflow import DAG
from airflow.operators.dummy import DummyOperator
from datetime import datetime

with DAG('loop_dag', start_date=datetime(2024, 1, 1)) as dag:
    tasks = []
    for i in range(3):
        task = DummyOperator(task_id=f'task_{i}')
        tasks.append(task)

print([task.task_id for task in tasks])
A['task_0', 'task_1', 'task_2']
B['task_1', 'task_2', 'task_3']
C['task_0', 'task_1']
D['task_0', 'task_1', 'task_2', 'task_3']
Attempts:
2 left
💡 Hint
Remember that range(3) generates numbers 0, 1, 2.
🔀 Workflow
intermediate
2:30remaining
Correct order of dynamic task dependencies in Airflow
Arrange the following code lines to correctly create a chain of tasks dynamically in Airflow using a loop.
A4,2,1,3
B4,1,3,2
C4,1,2,3
D1,4,2,3
Attempts:
2 left
💡 Hint
The loop must start first, then create task, set dependency, then update previous_task.
Troubleshoot
advanced
2:00remaining
Error caused by dynamic task creation in Airflow
What error will this Airflow DAG code raise when parsing, and why? from airflow import DAG from airflow.operators.dummy import DummyOperator from datetime import datetime with DAG('error_dag', start_date=datetime(2024, 1, 1)) as dag: for i in range(3): task = DummyOperator(task_id='task')
ANo error, DAG parses successfully
BSyntaxError: invalid syntax
CTypeError: DummyOperator() missing required positional argument
DAirflowDagValidationError: Duplicate task_id 'task'
Attempts:
2 left
💡 Hint
Task IDs must be unique in an Airflow DAG.
🧠 Conceptual
advanced
1:30remaining
Best practice for dynamic task creation in Airflow
Which statement best describes a best practice when generating tasks dynamically in Airflow using loops?
AAlways assign unique task_ids to each task to avoid conflicts.
BReuse the same task_id for all tasks to simplify the DAG.
CCreate tasks outside the DAG context manager to improve performance.
DAvoid using loops; create each task manually for clarity.
Attempts:
2 left
💡 Hint
Think about how Airflow identifies tasks uniquely.
Best Practice
expert
3:00remaining
Handling dynamic task dependencies with variable task counts
You want to create a dynamic chain of tasks in Airflow where the number of tasks is not fixed and depends on external input. Which approach ensures the chain is correctly built regardless of the number of tasks?
ACreate all tasks first, then outside the loop set dependencies manually between tasks by their index.
BInitialize a variable previous_task as None before the loop, then inside the loop, if previous_task is not None, set previous_task >> current_task, and update previous_task to current_task.
CUse a fixed number of tasks and ignore the external input to avoid complexity.
DSet all tasks to run in parallel without dependencies to simplify the DAG.
Attempts:
2 left
💡 Hint
Think about how to link tasks dynamically when you don't know how many there will be.