TimeDeltaSensor in Airflow: What It Is and How It Works
TimeDeltaSensor in Airflow is a sensor that waits for a specific amount of time to pass before allowing a task to proceed. It pauses the workflow for a defined time interval, acting like a timer to delay downstream tasks.How It Works
The TimeDeltaSensor works like a simple timer in your Airflow workflow. Imagine you want to wait for 10 minutes before starting the next task. Instead of running immediately, the sensor pauses the workflow until the set time has passed.
It checks the current time against the time when the sensor started plus the delay you set. Once the waiting period is over, it lets the workflow continue. This is useful when you want to add a fixed delay between tasks without relying on external events.
Example
This example shows how to use TimeDeltaSensor to wait for 5 minutes before running the next task.
from datetime import timedelta from airflow import DAG from airflow.sensors.time_delta_sensor import TimeDeltaSensor from airflow.operators.dummy import DummyOperator from airflow.utils.dates import days_ago default_args = { 'start_date': days_ago(1), } dag = DAG( 'timedelta_sensor_example', default_args=default_args, schedule_interval=None, ) start = DummyOperator( task_id='start', dag=dag ) wait_5_minutes = TimeDeltaSensor( task_id='wait_5_minutes', delta=timedelta(minutes=5), dag=dag ) end = DummyOperator( task_id='end', dag=dag ) start >> wait_5_minutes >> end
When to Use
Use TimeDeltaSensor when you need to delay a task for a fixed amount of time within your Airflow workflow. This is helpful if you want to wait for a system to stabilize, allow time for external processes to complete, or space out tasks to avoid overload.
For example, you might wait 10 minutes after a data load before starting data validation, or delay retries with a fixed wait time.
Key Points
- TimeDeltaSensor waits for a fixed time interval before continuing.
- It is a simple way to add delays without external triggers.
- Useful for pacing workflows or waiting for external systems.
- Configured by setting the
deltaparameter with atimedeltaobject.