Airflow Sensors
A sensor is a special task that waits for something to happen before the pipeline moves on. It checks a condition again and again until the condition is true or the time limit runs out. Sensors protect your pipeline from running too early, such as when a file has not arrived yet.
The Bus Stop Picture
Imagine you stand at a bus stop. You look down the road every few minutes. The moment the bus appears, you step forward and start your trip. If the bus never comes within an hour, you give up and take a taxi. A sensor behaves the same way.
Sensor task
┌────────────────────────────────────────┐
│ Is the file there? No → wait 60 sec │
│ Is the file there? No → wait 60 sec │
│ Is the file there? Yes → success │
└────────────────────────────────────────┘
│
▼
Next task starts
Why Sensors Help
Data rarely arrives at a fixed minute. A partner uploads a file at 6:10 one day and at 6:45 the next. A fixed schedule would start processing before the file exists and fail. A sensor waits for the real event instead of guessing the time.
A First Sensor Example
from datetime import datetime
from airflow import DAG
from airflow.sensors.filesystem import FileSensor
from airflow.operators.python import PythonOperator
def process_file():
print("Processing the sales file")
with DAG("wait_for_file", start_date=datetime(2024, 1, 1),
schedule="@daily", catchup=False) as dag:
wait = FileSensor(
task_id="wait_for_sales_file",
filepath="/data/incoming/sales.csv",
poke_interval=60,
timeout=3600,
mode="reschedule",
)
process = PythonOperator(task_id="process", python_callable=process_file)
wait >> process
Important Sensor Settings
| Setting | Purpose |
|---|---|
poke_interval | Seconds between two checks |
timeout | Total seconds to wait before the sensor fails |
mode | How the sensor uses a worker slot while it waits |
soft_fail | Marks the sensor as skipped instead of failed after a timeout |
deferrable | Hands the waiting to the triggerer |
Poke Mode and Reschedule Mode
Poke Mode
The sensor holds a worker slot for the whole wait. It checks, sleeps, and checks again without letting go. Poke mode suits short waits of a few minutes.
Reschedule Mode
The sensor frees its slot between checks and returns later. Many sensors can wait at the same time without blocking real work. Reschedule mode suits waits of many minutes or hours.
Poke mode: [██████████████████████████] slot busy all the time Reschedule mode: [█] [█] [█] [█] slot free between checks
Deferrable Sensors
Deferrable sensors go one step further. They hand the waiting job to the triggerer, a light service that watches thousands of events at once. The worker slot becomes free immediately. Many built-in sensors accept deferrable=True.
Popular Sensors
| Sensor | Waits For |
|---|---|
| FileSensor | A file on disk |
| ExternalTaskSensor | A task in another DAG |
| HttpSensor | A web address that returns a good response |
| S3KeySensor | An object in an Amazon S3 bucket |
| TimeDeltaSensor | A fixed amount of time |
| SqlSensor | A database query that returns rows |
Waiting for Another DAG
from airflow.sensors.external_task import ExternalTaskSensor
wait_for_upstream = ExternalTaskSensor(
task_id="wait_for_ingest",
external_dag_id="ingest_dag",
external_task_id="load_raw",
mode="reschedule",
)
Both DAGs need matching schedules, or the sensor must know how to map one run date to the other.
Custom Sensor Idea
Use the @task.sensor decorator when no ready-made sensor fits. The function returns True when the wait should end and False when it should keep checking.
Good Habits
- Always set a
timeout, so a missing file cannot block a DAG forever. - Choose reschedule or deferrable for any wait longer than a few minutes.
- Pick a sensible
poke_interval. Checking every second wastes resources. - Use
soft_failwhen a missing input is acceptable.
Key Takeaways
- A sensor waits for a condition and succeeds when it becomes true.
- Poke mode holds a slot, and reschedule mode releases it.
- Deferrable sensors scale best for long waits.
