spdl.pipeline.defs.Pipe

Pipe(op: Callable[[T], AsyncIterator[U]], /, *, concurrency: int = 1, executor: Executor | None = None, name: str | None = None, output_order: str = 'completion', max_failures: int | Fraction | None = None) → PipeConfig[T, U][source]
Pipe(op: Callable[[T], Awaitable[U]], /, *, concurrency: int = 1, executor: Executor | None = None, name: str | None = None, output_order: str = 'completion', max_failures: int | Fraction | None = None) → PipeConfig[T, U]
Pipe(op: Callable[[T], Iterator[U]], /, *, concurrency: int = 1, executor: Executor | None = None, name: str | None = None, output_order: str = 'completion', max_failures: int | Fraction | None = None) → PipeConfig[T, U]
Pipe(op: Callable[[T], U] | SupportsGetItem[T, U], /, *, concurrency: int = 1, executor: Executor | None = None, name: str | None = None, output_order: str = 'completion', max_failures: int | Fraction | None = None) → PipeConfig[T, U]
Pipe(op: Callable[[T], U] | Callable[[T], Iterable[U]] | Callable[[T], Awaitable[U]] | Callable[[T], AsyncIterable[U]] | SupportsGetItem[T, U], /, *, concurrency: int = 1, executor: Executor | None = None, name: str | None = None, output_order: str = 'completion', max_failures: int | Fraction | None = None) → PipeConfig[T, U]

Create a PipeConfig.

A pipe applys a function or mapping to the inocming item.

Parameters:
  • op –

    A function, callable or container with __getitem__ method (such as dict, list and tuple). If it’s function or callable, it is inovked with the input from the input queue. If it’s container type, the input is passed to __getitem__ method.

    The function or callable must take exactly one argument, which is the output from the upstream. If passing around multiple objects, take them as a tuple or use dataclass and define a custom protocol.

    If the result of applying op to an input item is None, the pipeline skips absorb the result and it won’t be propagated to the downstream stages.

    Optionally, the op can be a generator function, async function or async generator function.

    If op is (async) generator, the items yielded are put in the output queue separately.

    Warning

    If op is synchronous geneartor, and executor is an instance of concurrent.futures.ProcessPoolExecutor, the output items are not put in the output queue until the generator is exhausted.

    Async generator, or synchronous generator without ProcessPoolExecutor does not have this issue, and the yielded items are put in the output queue immediately.

    Tip

    When passing an async op, make sure that the op does not call sync function inside. If calling a sync function, use asyncio.loop.run_in_executor() or asyncio.to_thread() to delegate the execution to the thread pool.

  • concurrency – The maximum number of async tasks executed concurrently.

  • executor –

    A custom executor object to be used to convert the synchronous operation into asynchronous one. If None, the default executor is used.

    If op is async, the op still runs as a coroutine on the event loop, and the executor becomes the stage’s executor: the op runs its synchronous work there by calling run_in_stage_executor(). The executor is visible only to the tasks of this stage. concurrency keeps its meaning, the number of items in flight in the stage, while the executor’s max_workers bounds how many of their synchronous calls run at once. Without an executor, run_in_stage_executor() runs on the event loop’s default executor (the pipeline’s thread pool).

    Inside a .to() region, the same per-worker pool rules apply as for a synchronous stage. In a production data loader whose per-batch work (a cache read, a transform and the collation) ran in one async stage around an awaited fetch, removing the stage’s dedicated 1-thread executor, so that the work ran on the worker’s shared pool, made the loader about 9.6% slower.

    Inside a to() worker region, each worker builds its own copy of the executor: a ThreadPoolExecutor (or, on Python 3.14+, an InterpreterPoolExecutor) becomes a dedicated pool with the same max_workers in every worker, created on first use and shut down when the worker exits. Stages given the same executor object share one pool per worker, and such a stage does not count toward the size of the worker’s shared thread pool. The executor must be freshly constructed (no work submitted); other executor types raise an error when the pipeline is built.

    A dedicated pool is worth trying when synchronous work in an async stage, or a CPU-heavy stage, competes with other stages for the shared pool and you have measured that this costs time; see Dedicated thread pools for stages.

    Changed in version 0.9.0: Inside a .to() worker region, the stage runs on a dedicated pool built from this executor in each worker. Previously the executor was ignored there and the stage ran on the worker’s shared thread pool.

    Changed in version 0.9.0: An async op accepts an executor and makes it available through run_in_stage_executor(). Previously this was an error.

  • name – The name (prefix) to give to the task.

  • output_order – If "completion" (default), the items are put to output queue in the order their process is completed. If "input", then the items are put to output queue in the order given in the input queue.

  • max_failures – The maximnum number (int) or rate (Fraction) of failures allowed before the pipe operation is considered failure and the whole Pipeline is shutdown. When an int is provided, it specifies the maximum count of failures. When a Fraction is provided (e.g., Fraction(1, 10) for 10%), it specifies the maximum failure rate (failures / invocations). This overrides the value provided to the build() method.

Returns:

The config object.

See also

Example: Pipeline definitions

Illustrates how to build a complex pipeline.