Sensors and deferrable operators
Some tasks must wait for something outside Airflow: a file to land, a partition to appear, another pipeline to finish. Sensors do the waiting. This lesson shows how they work, and how to wait without blocking a worker slot.
Pipelines in production · Airflow · Silver · Control flow, lesson 3 of 3
On this page
Read first: Trigger rules and run state (skip if you know it)
In plain words first
Waiting for a parcel, you can stand at the door all day, or check every half hour and do other things in between, or ask to be called when it arrives.
A sensor waits in one of those ways. The last two, reschedule mode and deferrable sensors, free the workerworker: The process that actually runs a task instance, on whatever machine the executor sends it to. Learn more → while nothing is happening.
Waiting for something to be true
A sensor is a tasktask: One step of a DAG, such as running a query or calling an API. Each run of a task for one date is a task instance. Learn more → that waits: until a file lands, a partition appears, another DAGDAG: A pipeline in Airflow: a set of tasks and the order they run in, defined in a Python file and run on a schedule. Learn more →'s task finishes, or a time is reached. It checks a condition every poke_interval and succeeds once it is true. The tasks after it run only then.
from airflow.providers.standard.sensors.filesystem import FileSensor wait_for_statement = FileSensor( task_id="wait_for_statement", filepath="/landing/label_statements/{{ ds }}/_SUCCESS", poke_interval=300, # check every 5 minutes timeout=6 * 60 * 60, # give up after 6 hours mode="reschedule", # free the worker slot between checks ) wait_for_statement >> load_statement
Three ways to wait
| Mode | Between checks | Cost | Good for |
|---|---|---|---|
poke (default) | The task sleeps inside its worker slot | A worker slot for the whole wait | Waits of a few minutes |
reschedule | The task gives its slot back and is rescheduled for the next check | A slot only during each check | Waits of hours |
Deferrable (deferrable=True) | The wait moves to the triggerer, an async process that watches thousands of conditions | Almost nothing; needs a triggerer running | Many long waits |
A hundred sensors in poke mode waiting overnight hold a hundred worker slots doing nothing, and the tasks that should run cannot get a slot. Reschedule mode or deferrable operatorsoperator: A ready-made kind of task: BashOperator runs a command, PythonOperator a function. You make a task by giving an operator its arguments. Learn more → avoid that; prefer deferrable versions where the provider offers them.
Timeouts and soft failure
timeoutis how long the sensor may wait in total; the default is seven days, almost never what you want. Set it to when the data is late enough that someone should be told.- When the timeout passes, the sensor fails (and is not retried for that reason). With
soft_fail=Trueit is skipped instead, and so is everything that depends on it: right for "optional input, skip today if it never comes". exponential_backoff=Truelengthens the gap between checks, kinder to the system being polled.
Writing your own condition
from airflow.sdk import PokeReturnValue @task.sensor(poke_interval=600, timeout=4 * 60 * 60, mode="reschedule") def label_api_ready(ds=None) -> PokeReturnValue: status = fetch_status(day=ds) # your own check return PokeReturnValue(is_done=status == "complete", xcom_value=status)
Sensors or assets?
A sensor polls. When the data is produced by another Airflow DAG, there is a better way: that DAG declares the assetasset: Airflow 3's name for a piece of data a task updates. Other DAGs can be scheduled to run when it changes. Learn more → it updates, and your DAG is scheduled on the asset, so it starts when the data is ready, with no task waiting at all. Assets are covered in the Gold level; keep sensors for data that arrives from outside Airflow.
Common mistakes
- Long waits in poke mode — Each waiting sensor holds a worker slot.
- Leaving the default seven-day timeout — A missing file is noticed a week late.
- Sensors waiting on another Airflow DAG's output — Schedule on the asset that DAG produces instead.
- soft_fail on required inputs — The run is skipped quietly when it should fail loudly.
Key takeaways
- A sensor waits for a condition, checking every poke_interval.
- poke holds a worker slot; reschedule frees it between checks; deferrable moves the wait to the triggerer.
- Set timeout deliberately; soft_fail skips instead of failing.
- For data produced by other DAGs, schedule on assets instead of polling.
Interview prep
THE QUESTION
"Two hundred sensors wait overnight for partner files and other DAGs stop getting worker slots. What do you change?"
Avoid: "a sensor with the default settings". Default poke mode holds a worker slot for the whole wait, and the default timeout is seven days.
Follow-ups · answer out loud, then open
Follow-up"What does the triggerer do?"
Scenario"A DAG waits for a file from a partner that sometimes never arrives. Downstream reports are optional. How do you configure the sensor?"
Trap"A sensor in reschedule mode retries when it times out."
Check yourself
3 questionsWhich mode frees the worker slot between checks?
Show the answer
reschedule. Reschedule gives the slot back and is scheduled again for the next check.
What runs the waiting of a deferrable sensor?
Show the answer
The triggerer. Deferred tasks are watched by the triggerer's async loop.
A sensor with soft_fail=True times out. What happens to its downstream tasks?
Show the answer
They are skipped. The sensor is skipped and the skip propagates.
Keep going
Up next · new module: Scaling a DAG · 4 min readDynamic task mapping
One task per file, table or customer, decided at run time with expand().
Related lessons
Previous: XCom: passing data between tasks
Primary sources: Sensors · Deferrable operators and triggers