Skip to content
Silver 5 min read · Control flow

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

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.

Python · wait for a partner's file
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

ModeBetween checksCostGood for
poke (default)The task sleeps inside its worker slotA worker slot for the whole waitWaits of a few minutes
rescheduleThe task gives its slot back and is rescheduled for the next checkA slot only during each checkWaits of hours
Deferrable (deferrable=True)The wait moves to the triggerer, an async process that watches thousands of conditionsAlmost nothing; needs a triggerer runningMany 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

  • timeout is 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=True it is skipped instead, and so is everything that depends on it: right for "optional input, skip today if it never comes".
  • exponential_backoff=True lengthens the gap between checks, kinder to the system being polled.

Writing your own condition

Python · a sensor from a Python function
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?"

In poke mode each sensor occupies a worker slot for the whole wait. Switch them to reschedule mode, which frees the slot between checks, or better to deferrable sensors, which hand the wait to the triggerer process so no worker slot is used at all. Also set realistic timeouts so missing files alert in hours, not after the 7-day default, and use pools if sensors still compete with real work. If any of those files come from other Airflow DAGs, replace those sensors with asset scheduling.

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?"
It runs the waiting of deferred tasks in an asynchronous event loop: a deferrable sensor checks its condition, then hands a trigger to the triggerer and frees its worker slot. When the trigger fires, the task resumes on a worker.
Scenario"A DAG waits for a file from a partner that sometimes never arrives. Downstream reports are optional. How do you configure the sensor?"
reschedule or deferrable mode so the wait costs no worker slot, a timeout at the point where the report would be useless, and soft_fail=True so a missing file skips the run's dependent tasks instead of failing it. Alert separately if missing files should be followed up.
Trap"A sensor in reschedule mode retries when it times out."
Reaching the sensor's timeout fails it (or skips it with soft_fail); that is not a normal failure that uses retries. Retries apply to errors raised while checking.

Check yourself

3 questions

Which 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 read
Dynamic 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