Acting on the first of several

A task normally waits for all its upstream edges to be decided. To go on as soon as one of several has succeeded, use first_of, a node of its own that the master ends, like EmptyOperator: no process, no slot and no polling.

examples/docs/first_of.py
"""Acting on the first of several: `first_of`, `at_least`, and joining the paths of a `@branch`."""

from schedr import Dag, Data, Task, at_least, branch, first_of, task


@task
def from_cache() -> str:
    """One way to get the prices."""
    return "cached prices"


@task
def from_service() -> str:
    """Another way to get the same prices."""
    return "fresh prices"


@task
def price(value: str, winner: str) -> str:
    """Runs as soon as one of them has its value; the other runs on."""
    return f"{value} from {winner}"


@task
def region(name: str) -> int:
    """Asks one region to accept the write."""
    return len(name)


@task
def accepted(sizes: list[int], winners: list[str]) -> str:
    """Runs as soon as two regions have answered, in the order they did."""
    return f"{len(sizes)} regions: {', '.join(sorted(winners))}"


class Report(Data):
    """Where a report went."""

    path: str


@task
def as_pdf() -> Report:
    """The path chosen."""
    return Report("report.pdf")


@task
def as_html() -> Report:
    """The path not chosen: it is skipped."""
    return Report("report.html")


@task
def publish(report: Report) -> str:
    """Gets the one value there is, not one parameter per path."""
    return f"published {report.path}"


with Dag(
    "first_of_demo",
    description="Goes on with the first of several sources to answer, and joins a branch's paths.",
):
    value, winner = first_of(from_cache(), from_service(), with_winner=True)
    price(value, winner)

    sizes, regions = at_least(
        2,
        region.override(task_id="eu")("eu"),
        region.override(task_id="us")("us"),
        region.override(task_id="asia")("asia"),
        with_winner=True,
    )
    accepted(sizes, regions)

    pdf = as_pdf()
    html = as_html()

    @branch
    def choose() -> list[Task]:
        return [pdf]

    choose() >> [pdf, html]
    publish(first_of(pdf, html, task_id="report"))

first_of

publish(first_of(a, b)) passes on the output of the task that succeeded first. value, winner = first_of(a, b, with_winner=True) also gives the winner’s task ID. Type checkers read the first as the tasks’ value type and the second as (value, str). To order a task after the node without its value, use chain(first_of(a, b), x).

Narrow a task with .on(...) to say which outcomes count: with first_of(a.on(str), b.on(str)), a wait step that timed out doesn’t win. A skipped task doesn’t win either, so first_of also joins the paths of a @branch, and the task after it gets the one value there is.

The tasks given

The node

one has succeeded, on an outcome that counts

success at once, with its value

none has yet, and some still can

waits

none can, and one failed

upstream_failed

none can, and one was cancelled

cancelled

none can, and all were skipped or ended on another outcome

skipped

Nothing is cancelled. The others run on as any task. One that fails later fails the run when the run ends, but the node and what follows it stay as they ended.

at_least

accepted(at_least(2, a, b, c)) waits for two of them and passes their values on as a list, in the order the tasks ended; with_winner=True also gives the list of their IDs. The rest is as for first_of, with “can” meaning enough of them still can.

Limits

at_least(n, ...) requires 1 <= n <= the number of candidates. A value outside that range raises ValueError.

  • Give at least two different tasks, each whole: a.rows is a part of an output and is refused. Another node’s value counts as whole; its winner doesn’t.

  • A mapped task is refused: choose from individual, unmapped tasks.

  • Its output limit is the sum of the candidate tasks’ max_output_size values. The stored result includes the values and winner IDs; if that whole result exceeds the sum, the node fails. Its own max_output_size does not apply.

  • Nothing can be ordered before the node: x >> first, or x >> g for a group g that starts with it, is refused when the file loads. Order the tasks it was given instead.

  • at_least reads a list as it is stored: two classes with the same fields, as outcomes of a task, can’t be told apart in its list. first_of tells them by its winner’s outcome.

  • The node’s ID is first_of or at_least, numbered from the second on; task_id= names it.

In the graph

On a run’s page the tasks a node chooses from sit in one box, with one dashed line to the node, since they are treated as a group and aren’t each a dependency. The winner is outlined and named on the node. A task given to two nodes, or tasks at different depths or in different groups, can’t share a box, so each gets its own dashed line.