Five ways to say "what runs per batch". Same fan-out, same batching, same recovery — only the per-batch executor changes.
The five are exactly _VALID_MODES in python/forkrun/_api.py:
python, spawn, plugin, splice, udf. This file previously
enumerated only four; udf was reachable and tested but absent from
the list, which is the same class of drift the doc-accuracy gate exists
to catch. udf is documented in its own section below because its
payload form, helper requirement and route set all differ.
Your payload is a Python function. Use it for custom logic: parsing, validation, feature extraction, filtering.
import json
def clean(batch):
out = []
for line in bytes(batch.data).split(b"\n"):
line = line.strip()
if not line:
continue
try:
rec = json.loads(line)
except ValueError:
continue # malformed rides the retry path, then poisons
if rec.get("duration_ms", 0) >= 100:
out.append(json.dumps(rec).encode())
return b"\n".join(out) if out else None
forkrun.map(clean, "events.jsonl", workers=8, order="index")- Payload forms: a callable (fork-inherited, never pickled)
or
"pkg.mod:func". A string spec is resolved in the parent on this path, before any fork: a child forked from a threaded host must not import, because frozen import locks in the child deadlock. The cleanroom UDF path (mode="udf", ormode="python"with the helper built) is the exception — there the spec is resolved inside the launcher afterexec. See_worker.pyand_udf.py. - Throughput (28 workers, 5M records): ~1.6M/s light,
~700k/s medium. An earlier version of this file called that "at parity
with
ProcessPoolExecutor", which is now wrong in the other direction: with static file partitioning -- the standard Python idiom -- mediumProcessPoolExecutormeasures 778k andmultiprocessing.Pool775k, so forkrun's Python UDF path leads by ~1.00-1.19x. Batching on the fly would narrow that further and is available to them; it is not quoted because forkrun does it automatically. Seepython/benchmarks/results/RELEASE_v3.6.0.mdsection 0.
A Python callable referenced by module, executed by an interpreter that lives inside the cleanroom launcher rather than in your process.
These are two different calling conventions that share a language:
mode="python" |
mode="udf" |
|
|---|---|---|
| Payload | callable, or "pkg.mod:func" |
"pkg.mod:func" only |
| What the payload receives | a forkrun.Batch (data plus offsets, line counts, metadata) |
a single bytes object |
| What it may return | bytes / memoryview / str / None | bytes or None |
| Where the callable is resolved | in your process, before any fork | inside the launcher, after exec |
A mode="udf" function therefore has no access to Batch fields, and a
mode="python" function must not assume it receives raw bytes. Passing a
callable to mode="udf" raises ValueError rather than being coerced.
-
A version-matched helper
.sois required for the isolated path. Whenever the isolated path is not selected for the call — the helper is not built for this minor, or the cleanroom is off (the default), or the call is not one the launcher serves —mode="udf"still works, but it degrades: the spec is resolved in your process and the callable runs in-process through themode="python"worker, wrapped by an adapter that restores the bytes-in / bytes-or-None-out contract. The adapter exists because a bare fallback would silently hand aBatchto a function written forbytes— every batch would raise, retry and poison, and the caller would see an empty result with no error naming the cause.The helper's existence is not what decides this. The route selection does: a helper built for this minor says the isolated path is possible, not that it was chosen. When the two were conflated,
mode="udf"raisedNotImplementedErrorin the default configuration — a fallback that raises is not a fallback. -
Routes: served wherever
mode="plugin"is served — the same launcher and the same envelopes. Selectable as themap-udfandstream-udfroutes.stream(spawn)-style unavailability does not apply, but neither doessplice-style availability:stream(udf)requiresorder="none", andstream(udf, order="index")declines and falls back in-process. -
Not served in the plain non-cleanroom worker loop as a distinct mode; there it is the in-process degradation above — which is a working call, not an error.
Implementation: python/forkrun/_udf.py, fr_udf.c,
fr_udf_helper.c, fr_udf_fork.c. Tests: test_udf_mode.py,
test_udf_helper.py, test_udf_exactly_once.py,
test_udf_stream_liveness.py, test_udf_version_barrier.py.
Your payload is an external command. Use it for existing CLI
tools — gzip, sed, tr, grep, your own binaries.
forkrun.map("gzip -c", "logs.txt", mode="spawn")
forkrun.map(["sed", "s/old/new/"], "data.txt", mode="spawn")stris split on whitespace; use thelistform whenever quoting would matter.- Batch bytes go to the command's stdin; stdout is captured as the result. Non-zero exit rides retry-then-poison like any payload error (missing commands exit 127, same path).
c_spawn_loop=True(opt-in,map()only, UMA only) runs the claim→spawn→ack loop in C. Measured at parity with the Python loop (±4%) — its value is architectural uniformity (one engine-owned recovery path), not speed.
Your payload is a C function in a .so. Use it for maximum
throughput when you can write C.
forkrun.map("./myplugin.so:process", "data.txt", mode="plugin")- Two conventions, not one: the default v0 Python path calls
a one-arg entry point,
int process(struct fr_py_plugin_ctx *ctx)(72B Python-side buffers, return code captured — NOT the frozen engine ABI). Only dialect-tagged plugins (forkrun_use_ctx1/2) dispatch through the C-level v1 path on the frozen ABI (struct forkrun_ctx, 128B: the plugin reads the input window zero-copy and writes stdout, which the shim stages and frames) — and only those work from both bash-Cand Python unchanged. An untagged legacy two-arg bash plugin called through v0 would receive the ctx pointer asargcand garbage asargv; the parent probes the tag and never guesses. See PLUGINS.md. - Throughput (28 workers, 5M): 5.5M/s light, 2.3M/s medium (yyjson single-pass), 710k/s heavy — 2–7× the Python loop.
c_worker_loop=True(opt-in,map()only, dialect-1/2 plugins, UMA only) moves the whole worker loop into C.- Writing one: PLUGINS.md.
Kernel passthrough — no computation, just data movement. Use it to split/parallelize I/O at memory bandwidth.
forkrun.map(None, "input.bin", mode="splice", bytes=512*1024)
for chunk in forkrun.stream(None, "input.bin", mode="splice"):
process(chunk) # raw byte-windows as they arrive- Payload must be
None; byte batching only (bytes=N, default 512KB —lines=is rejected, boundaries are never detected);run()is rejected (passthrough produces output — usemap()/stream()). - Workers run the claim→sendfile→ack loop with zero Python per batch.
| Situation | Mode |
|---|---|
| Custom Python logic | python |
| Existing CLI tool or binary | spawn |
| Hot loop worth writing in C | plugin |
| Just moving bytes | splice |
| Per-batch Python overhead matters | plugin + c_worker_loop, or spawn + c_spawn_loop |
Throughput expectations (honest, same-boot measured): PERFORMANCE.md.