pool
result = pool(n)
Input: n · Output: result
What it is
n (integer, number of local cpu workers).
Overloads
2 forms. Each carries its own inputs, outputs and worked example — pick the tab for the one you are calling.
| Code | Input | Output | Description |
result = pool(n) | n numbernot documented yet | result — | pool <name> with cpu=N ... end pool is a statement, not an expression — it registers a persistent pool under the bare identifier name, so more than one later run ... on name call (or run ... on any) can share the same fixed set of worker threads instead of each spinning up its own. |
Examples
Written for this form and run by the build -- the output below is what the engine actually produced.
square(x) := x * x
pool workers with cpu=2
end pool
q = queue()
q.push("square", 3)
print(run q on workers) # (9)
(9)
| Code | Input | Output | Description |
result = pool(n) | n numbernot documented yet | result — | end pool registration step at all. |
Examples
Written for this form and run by the build -- the output below is what the engine actually produced.
square(x) := x * x
q = queue()
q.push("square", 3)
print(run q on pool(2)) # (9)
(9)
A named pool is worth registering when more than one run call — maybe across different stages of a script — should share the same fixed set of worker threads rather than each spinning up its own. The example below defines a pure function, builds a 4-worker pool named workers, queues five calls to it with .push, then drains the whole queue at once with run ... on, printing the resulting list of squares in submission order:
square(x) := x * x
pool workers with cpu=4
end pool
q = queue()
for i in 0 to 4
q.push("square", i)
end for
results = run q on workers
print(results) # (0, 1, 4, 9, 16)
(0, 1, 4, 9, 16)
The anonymous form skips the pool ... end pool registration entirely -- handy when nothing else needs to share the same workers, since pool(n) can be written directly inline as the target of run ... on:
square(x) := x * x
q = queue()
for i in 0 to 4
q.push("square", i)
end for
results = run q on pool(2) # 2 local cpu workers, scoped to this one call
print(results) # (0, 1, 4, 9, 16)
(0, 1, 4, 9, 16)
How it works
pool(n) (§47.3) — the anonymous pool shorthand: n cpu workers, fixed round_robin policy, on full = queue, scoped to whichever single run call uses it (never registered — see Interp::pool_registry's doc comment). The NAMED form (pool <name> with cpu = ... end pool) is a statement (Stmt::Pool, executed directly in exec), not this builtin — this arm only ever fires for the bare-call shorthand pool(8) used as an expression (typically right inside run jobs on pool(8)).
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.