Execution lifecycle
Experimental — breaking changes may land in any release
Pin your version and read the changelog before upgrading.
A built Pipeline offers five execution primitives. They differ in one critical
way: only run() consumes the source end-to-end under the durability
contract; everything else is bounded sampling and never rewinds offsets or
state.
| Primitive | Behavior | Semantics |
|---|---|---|
run() | Blocks until end-of-stream or SIGINT (graceful cancel). The GIL is released while waiting. | End-to-end delivery. The only primitive that consumes a sink under the durability contract. |
take(n) | Returns up to n batches, then cancels. | Sampling. Abandoning does not rewind offsets/state. |
collect(duration=seconds) | Runs at most duration, then cancels. | Sampling. |
iter_batches() | Lazy iterator; abandoning cancels; bounded prefetch. Honors KeyboardInterrupt per __next__. | Sampling. |
start_background() | Non-blocking; returns a PipelineHandle with metrics() and idempotent stop(). | Ctrl-C does not reach background pipelines — call stop(). |
Choosing a primitive
- Production consumption —
run(). It propagates backpressure and only acknowledges sources after sinks confirm. - Preview / test —
take(n)orcollect(duration). - Stream processing in a loop —
iter_batches(); each__next__releases the GIL. - A pipeline beside other Python work —
start_background(); pollhandle.metrics()and stop it explicitly before exiting.
Metrics
PipelineHandle.metrics() returns a dict keyed by stream id with state,
input_batches, output_batches, and errors. The last snapshot stays valid
after stop():
handle = flow.start_background()
...
handle.stop()
print(handle.metrics())
Cancellation and reentrancy
- SIGINT during
run()triggers graceful cancellation; the call returns instead of killing the process. - Re-entering the engine from inside a UDF (calling
run/collect/takeon a pipeline while it is executing) raisesExecutionError. Build a separate pipeline instead. - A
Pipelineobject can be executed repeatedly; connector-backed runs resume from persisted state.
Verification
flow = arkflow.Pipeline().from_iter([{"v": i} for i in range(10)]).to_stdout()
batches = flow.take(3)
assert len(batches) == 3 # bounded sampling
flow.run() # full end-to-end consumption (re-execution is fine)