# Wait steps A wait step waits for one thing belonging to a run, then ends with its value or the `TimedOut` outcome. Declare an `async def` with `@sensor` when you write the waiting yourself, or a function with `@sensor.poke(every=...)` when `schedr` should call it repeatedly: an `async def`, or a plain `def` (see [A plain def poke](#a-plain-def-poke)). To wait for another DAG's run or task, use [`wait_for(RunOf(...))`](21-wait-for.md), which can also produce `Failed`. [Wait steps and triggers side by side](19-triggers.md#wait-steps-and-triggers-side-by-side) compares every form. ```{literalinclude} ../../examples/docs/sensors.py :language: python :caption: examples/docs/sensors.py ``` A poke returns its value when ready, or `NOT_YET` while waiting: annotate it `-> str | NotYet`. `None` is a value, not a signal to wait. Calls never overlap. A call that runs past `every` is an overrun (its own time counts, not a stall of the event loop after it): it may finish, and the next call comes right after it. The poke fails (`PokeTooSlowError`, with the usual retries) after `[watchers] poke_overruns` overruns in a row while returning `NOT_YET` (3 by default), or at once when one call runs three times `every`, when it is cancelled. A call that returns its value within that hard limit ends the wait successfully. A call that raises is a failure with the usual retries too. A call that holds the event loop is another limit: see below. ## A plain def poke A poke may be a plain `def`, for a blocking client: `schedr` calls it on the host's threads, as `asyncio.to_thread` would, so it doesn't hold up the other wait steps on the host. The call belongs to its wait step, as an `async` call does: its prints go to the step's log, `task_ctx` and the step's secrets work in it (`task_ctx.secret(...)`, the plain form), and its call has the same window as an async poke's (it may run past `every`, up to three times it). Past that, the call is stopped as an `asyncio.to_thread` call is (see below): `StepEndedError` at its next Python line, or, for a call blocked in C, its host ending after the cancel grace. A plain `def` opens a plain connection (`with name() as client:`). A plain `def` that returns a coroutine (a plain wrapper of an `async def`) has it awaited. `@sensor` itself, and `wait_for=`, stay `async def`. ```{literalinclude} ../../examples/docs/plain_pokes.py :language: python :caption: examples/docs/plain_pokes.py :lines: 19-47 ``` Which to choose: - **An async client** where there is one: many pokes wait on one thread, and a call is cancelled where it awaits. - **A plain `def`** for a blocking client (a database driver, `requests`, a cloud SDK without async), rather than an `async def` that blocks the event loop, which holds up every wait step on the host and is killed past `[watchers] block_limit`. - **Give each blocking call a timeout of its own**, below `every`: a call that ends by itself is always cleaner than one stopped from outside, which may leave a library's state half updated. - **Plain pokes run side by side**, each on a thread of its own, so what they share (module globals, a client kept between calls) must be thread-safe. - **Keep them light.** A poke busy in Python, not waiting, holds the interpreter's lock and slows every other wait step on the host. Put heavy work in a task. The host sizes its thread pool for one call per wait step, plus Python's default pool size for the steps' own `asyncio.to_thread` calls. Threads start as needed. This gives plain pokes room to run alongside one another; it does not guarantee call timing. `max_wait=` limits the wait. If omitted, `[sensors] default_max_wait` applies (24 hours by default), except for `delay` given a `timedelta`, as described below. Set `max_wait` in `StepDefaults` for a DAG or group; `timeout` there applies to task attempts. When the wait expires, `schedr` produces `TimedOut`: leave it out of the function's return type. ## Waiting for a time Import `delay` and `until` from `schedr.sensors` for built-in wait steps: ```{literalinclude} ../../examples/docs/wait_times.py :language: python :caption: examples/docs/wait_times.py ``` `delay(timedelta(...))` waits from its first try's start. If tried again, it waits only what is left. `until(timedelta(...))` waits until that duration after the run's `planned_for`; `until(datetime)` waits until that moment, which must have a time zone. A moment already past ends at once. A run parameter can supply either argument. Both steps return the moment they ended as a `datetime`; use `.on(datetime)` to pass it to a task. They produce `TimedOut` past `max_wait`. For `delay` given a `timedelta`, the default `max_wait` is that duration plus one minute; otherwise the usual wait step default applies. Their IDs are `delay` and `until`, numbered from the second call (`delay_2`, `until_2`, ...); set `task_id=` to choose one. ## What downstream tasks see Suppose `file` is a call of a wait step returning `str`, and `load` takes `path: str`. The call reads as `Waited[str]`: it can produce a value or `TimedOut`. | Expression | What happens | |---|---| | `load(file.on(str))` | Runs with the value; the edge is not taken on timeout. | | `file.on(TimedOut) >> alert()` | Runs `alert` on timeout. | | `handle(file)` with a parameter `str \| TimedOut` | Receives either; write `handle(file.on(str, TimedOut))` for type checkers. | | `file >> x` | Orders `x` only when the wait has its value. | | `chain(file.on(str, TimedOut), x)` | Orders `x` after either outcome. For a value type without `>>`, such as `str`, type checkers need `chain`. | | `load(file)` with `path: str` | Refused at parse and by type checkers: the timeout is also possible. | A failed wait step is not a timeout: downstream tasks end `upstream_failed`. When a downstream task has another upstream task, `run_if` decides what happens when an edge is not taken, as described in [Routing by outcome](08-outcomes.md). A timeout leaves the run `success` whether or not a task takes it. Use `.on(TimedOut)` to do something about it. A task normally waits for its upstream edges to be decided before it runs. To act on the first of several, as `fast_report` does above, use [`first_of`](20-first-of.md). ## Running and deploying wait steps This section describes sensors. The master ends `wait_for(RunOf(...))` without a watcher. Wait steps using the same interpreter and code copies run as coroutines in one long-lived process, the watcher host. It stops when its last wait step ends. A DAG file's top level is imported once per host, so module globals are shared between its wait steps. The working directory and environment belong to the host, not a run: use absolute paths and read run values from `dag_ctx` and `task_ctx`, never `SCHEDR_*` environment variables. Use asynchronous clients and yield while waiting: a blocking call holds up the other wait steps on the host. If one holds the loop past `[watchers] block_limit` (30 seconds by default), its host is killed and it fails `blocked_event_loop` without a retry. The other wait steps start again on a new host in the same attempt, with their `max_wait` still running and no retry used. Put heavy work in a task. Read declared secrets with `await task_ctx.asecret(...)` or `await task_ctx.asecret_value(...)`; the plain forms hold the loop when a secret under a `SecretPrefix` must be fetched. See [Secrets](13-secrets.md#reading-a-secret-inside-a-task). A wait step can't start a process or a thread of its own: nothing could stop what it left running while other wait steps keep the host alive, and a retry could overlap it. Use an async client or `asyncio.to_thread`, or run a command in a task after the wait step. - **When its DAG is defined**, so `schedr dags check` says so before you deploy: a wait step's code using `subprocess`, `os.system`, `os.fork`, `os.spawn*`, `os.exec*`, `multiprocessing`, `ProcessPoolExecutor`, `asyncio.create_subprocess_*`, `threading.Thread`, `threading.Timer` or `ThreadPoolExecutor`, or a function, decorator or class of your own that does, is an import error naming the call and its line. A class named in `isinstance`, `issubclass` or an annotation is fine. This reads what the code names, so it can miss a value it can't follow. - **When it runs**, for what can't be read that way (a library's insides, `getattr`, a function passed in): starting a process raises `ProcessInWaitStepError`, from a thread of the step too. The standard library's `platform` and `ctypes.util` helpers still work, as client libraries call them and they wait for their command. A library may start threads; nothing refuses those. This isn't a sandbox: C code gets past both, and what it leaves running lives until the host stops. `asyncio.to_thread` belongs to its wait step: its prints go to the step's log, `task_ctx` works in it, and it runs out of time with the step. When the call is cancelled (`max_wait`, three times `every`, a cancel, your own `asyncio.timeout`) or the step ends with the call still running, `StepEndedError` is raised in its thread at its next Python line. It isn't an `Exception`, so `except Exception` doesn't stop it, and `finally` runs. Python can't reach a thread blocked in C (`time.sleep(600)`, a read with no timeout): if it hasn't ended after the cancel grace, the step's result is reported and the host is killed; the other wait steps start again as described above. Give blocking calls a timeout of their own, and put heavy work, such as a large query in a library with its own threads, in a task. That error can land on any Python line, also halfway through a library updating its own state, such as a connection pool or a cache. `with` and `finally` run, but code with no cleanup for it stays half done, and the other wait steps of the same code share that library in their process. So a call that has a timeout of its own, and ends by itself, is always the safer way to stop. When a step ends, asyncio tasks it left running are cancelled and awaited. `print` and Python logging from the step go to its attempt log, as does a thread it started; output from C code, or from a thread still running after its step ended, goes to the host's log, masked for secrets. Unmanaged library threads may live until the host stops; make sure the library closes them when the step ends. The step's log warns if a library leaves a thread running. When new code arrives, a `@sensor.poke` moves between calls if it can start on that code. It keeps its deadline and uses no retry; its log says it moved and the attempt reports the code it moved to. If the task was renamed or removed, the file cannot be imported, or the new signature does not fit the run's arguments, it stays on its old code and its log says why. A full-control `@sensor` stays on its code until it ends, keeping its host and code copy: frequent deploys with very long waits can therefore cost memory. A retry starts on the newest code. A wait step takes a watcher slot (`[watchers] slots`, 500 per worker by default), not a task slot. Its `max_wait` runs from its first start, over retries and moves. A draining worker hands its pokes to others at once, using no retry; a full-control `@sensor` gets the drain's grace time, then runs again elsewhere, also using no retry. Cancellation gives it the usual grace time; if it does not end, its host is killed and the other wait steps start again as above. A host crash is handled differently; see [The watcher host](../03-guide/05-workers.md#the-watcher-host). After `[watchers] switch_off_after` host kills in a row (default 3), a wait step is switched off: new attempts fail `switched_off` without reaching a host. A success resets the count. New code gives it one chance: another strike switches it off again until a success. List these steps with `schedr sensors switched-off` and switch one on with `schedr sensors switch-on `. The DAG page lists its switched-off wait steps and has a **Switch on** button; the DAGs list shows a banner with their count and links to their DAGs. ## Limits for now `cleanup=` is refused; use `finally` in the function. `.expand()` on a wait step is refused. `preload` and `fresh_process` are not options. A function that ignores cancellation at `max_wait` is stopped past the grace period and fails `timed_out`. ## Testing `await wait.fn(...)`, or `asyncio.run(wait.fn(...))`, calls the function itself. For a poke this is one call, which may return `NOT_YET`; a plain `def` poke's `.fn` runs it on a thread, as its host does, so it is awaited the same way. `run_dag` runs the wait step for real; see [Testing a DAG](07-testing.md).