loop: One Step, Many Items

REAL

NoETL has two ways to repeat work, and both are real:

  1. loop: on a step runs that step once per item of a collection (this section).
  2. Back-edges: an arc that goes back to an earlier step, guarded by when (6.4).
# Chapter 6.3: a parallel loop. The server dispatches ALL items at once;
# max_in_flight is parsed but nothing reads it (server v3.161.2).
apiVersion: noetl.io/v2
kind: Playbook
metadata:
  name: loop_parallel
workload:
  cities: [Lisbon, Oslo, Quito]
workflow:
  - step: start
    loop:
      in: "{{ workload.cities }}"
      iterator: city
      spec:
        mode: parallel
        max_in_flight: 1              # no effect: all three are dispatched together
    tool:
      kind: python
      args:
        city: "{{ iter.city }}"       # also {{ city }}, {{ iter._index }}, {{ iter._total }}
      code: |
        result = {"city": args["city"], "len": len(args["city"])}
    next:
      - step: summarize
  - step: summarize                   # here {{ start }} is the loop's result: a list
    tool: { kind: noop }

The keys

KeyMeaning
loop.inA template that evaluates to a list. Required for sequential and parallel.
loop.iteratorThe name each item gets. Must not be empty.
loop.spec.modesequential (the default), parallel, or cursor
loop.spec.max_in_flightParsed, and ignored (see below)

Inside the loop body each item is available as {{ iter.<iterator> }} and also as plain {{ <iterator> }}, together with {{ iter._index }} and {{ iter._total }}, which are also available plain as _index and _total. The same values are injected into the tool's args (build_iteration_command, orchestrate-core/src/commands.rs, v3.161.2). There is no loop.index.

Sequential and parallel

These were checked in orchestrator.rs at v3.161.2:

  • sequential dispatches item 0. Each time an item completes and nothing else of the loop is in flight, it dispatches the next. One slot at a time.
  • parallel dispatches every item at once: dispatch_count = if is_parallel { total } else { 1 }. A 500-item parallel loop puts 500 commands on the bus in one pass. They run as fast as there are free worker slots, so a large loop can occupy a whole pool.

Myth

"max_in_flight limits how many items of a parallel loop run at once." It does not. At server v3.161.2, loop.spec.max_in_flight is parsed into the grammar and nothing reads it: not the orchestrator, not the server, not the worker. The server's resource model says the same: "max_in_flight is parsed but has no reader, so it does not throttle anything". To bound concurrency today, use sequential mode, split the collection across steps, or size the worker pool (Chapter 10).

The loop's result

When the last item completes, the step completes. Its result is a list of each iteration's result, in the order the iterations completed. In parallel mode that need not be the order of in:; the code says so itself: "may not match dispatch index in parallel mode" (state.rs). If order matters, have each iteration return its own index or key.

An empty collection completes the step at once, with the result [] and no commands dispatched.

cursor mode

mode: cursor is for draining a work queue rather than a fixed list. Instead of in:, the loop has a cursor.claim query (a postgres tool by default) that atomically leases a batch of rows, for example with FOR UPDATE SKIP LOCKED. The body runs once per claimed row, loop.spec.frame.row_concurrency rows at a time (default 1). When the batch is done, the next one is claimed. A claim that returns no rows ends the loop. frame.max_rows caps each claim. The lease and heartbeat fields are documented as advisory. Cursor mode is used only when a playbook asks for it.

On the local runtime

```admonish warning title="The local runtime ignores loop:" At CLI v6.4.1 the local runtime parses loop: but has no code that reads it. A loop step runs once, as if loop: were not there, and the run ends ok. CI checks this (examples/ch08/loop-runs-once). Locally, iterate with a back-edge (6.4) or inside one step: a set-based DuckDB query, or a Rhai script. Chapter 8 covers local iteration.


The examples on this page are distributed. CI validates them against
the schema but does not run them.

## Check your understanding

<div class="quiz-placeholder" data-quiz-name="&quot;ch06-03&quot;" data-quiz-questions="{&quot;questions&quot;:[{&quot;context&quot;:&quot;Parallel mode dispatches every item at once, and `max_in_flight` has no\nreader at server v3.161.2. Slots then decide how many RUN at once, but all\n400 are queued.\n&quot;,&quot;id&quot;:&quot;623e6c5d-c981-49c2-8942-de68d1737040&quot;,&quot;type&quot;:&quot;MultipleChoice&quot;,&quot;answer&quot;:{&quot;answer&quot;:&quot;400&quot;},&quot;prompt&quot;:{&quot;distractors&quot;:[&quot;10&quot;,&quot;1&quot;,&quot;As many as there are free slots.&quot;],&quot;prompt&quot;:&quot;A parallel loop over 400 items sets `max_in_flight: 10`. How many iteration\ncommands does the server dispatch in the first pass?\n&quot;}},{&quot;context&quot;:&quot;Results are appended in arrival order, which \&quot;may not match dispatch index in\nparallel mode\&quot; (`state.rs`). Return an index or key from each iteration if\norder matters.\n&quot;,&quot;id&quot;:&quot;9e6dbfb4-10c1-49e1-9ebb-eda84344db2a&quot;,&quot;type&quot;:&quot;MultipleChoice&quot;,&quot;answer&quot;:{&quot;answer&quot;:&quot;The order the iterations completed.&quot;},&quot;prompt&quot;:{&quot;distractors&quot;:[&quot;The order of the `in:` collection.&quot;,&quot;Sorted by item value.&quot;,&quot;Random, reshuffled on each read.&quot;],&quot;prompt&quot;:&quot;In what order are a parallel loop's results in its aggregated list?&quot;}},{&quot;context&quot;:&quot;The server binds the item both under `iter.` and at the top level, with\n`_index` and `_total`. There is no `loop.*` namespace.\n&quot;,&quot;id&quot;:&quot;1c466bf2-1891-480f-8fd1-e0928364a79a&quot;,&quot;type&quot;:&quot;MultipleChoice&quot;,&quot;answer&quot;:{&quot;answer&quot;:&quot;`{{ iter.city }}` (or plain `{{ city }}`)&quot;},&quot;prompt&quot;:{&quot;distractors&quot;:[&quot;`{{ loop.item }}`&quot;,&quot;`{{ loop.city }}`&quot;,&quot;`{{ item }}`&quot;],&quot;prompt&quot;:&quot;How does a loop body refer to the current item when `iterator: city`?&quot;}}]}" data-quiz-cache-answers="true"></div>


<script type="text/javascript" src="quiz/quiz-embed.iife.js"></script><link rel="stylesheet" type="text/css" href="quiz/style.css">