loop: One Step, Many Items
REAL
NoETL has two ways to repeat work, and both are real:
loop:on a step runs that step once per item of a collection (this section).- 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
| Key | Meaning |
|---|---|
loop.in | A template that evaluates to a list. Required for sequential and parallel. |
loop.iterator | The name each item gets. Must not be empty. |
loop.spec.mode | sequential (the default), parallel, or cursor |
loop.spec.max_in_flight | Parsed, 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:
sequentialdispatches 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.paralleldispatches 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.
"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=""ch06-03"" data-quiz-questions="{"questions":[{"context":"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","id":"623e6c5d-c981-49c2-8942-de68d1737040","type":"MultipleChoice","answer":{"answer":"400"},"prompt":{"distractors":["10","1","As many as there are free slots."],"prompt":"A parallel loop over 400 items sets `max_in_flight: 10`. How many iteration\ncommands does the server dispatch in the first pass?\n"}},{"context":"Results are appended in arrival order, which \"may not match dispatch index in\nparallel mode\" (`state.rs`). Return an index or key from each iteration if\norder matters.\n","id":"9e6dbfb4-10c1-49e1-9ebb-eda84344db2a","type":"MultipleChoice","answer":{"answer":"The order the iterations completed."},"prompt":{"distractors":["The order of the `in:` collection.","Sorted by item value.","Random, reshuffled on each read."],"prompt":"In what order are a parallel loop's results in its aggregated list?"}},{"context":"The server binds the item both under `iter.` and at the top level, with\n`_index` and `_total`. There is no `loop.*` namespace.\n","id":"1c466bf2-1891-480f-8fd1-e0928364a79a","type":"MultipleChoice","answer":{"answer":"`{{ iter.city }}` (or plain `{{ city }}`)"},"prompt":{"distractors":["`{{ loop.item }}`","`{{ loop.city }}`","`{{ item }}`"],"prompt":"How does a loop body refer to the current item when `iterator: city`?"}}]}" 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">