Dedicated thread pools for stages

By default, the synchronous stages of a pipeline share one thread pool: the pipeline’s pool in the main process, or the worker’s pool inside a to() region. Each call runs on whichever thread of the pool is free. Giving a stage its own ThreadPoolExecutor runs its calls on that pool’s threads instead, so the stage no longer competes with the other stages for the shared pool.

When it helps

A dedicated pool helps when synchronous work competes with other stages for the shared pool:

  • Synchronous work inside an async stage. An async op that offloads its decode, transform or collation with run_in_stage_executor() runs that work on the shared pool unless the stage has an executor, so it waits behind every other stage’s calls.

  • A CPU-heavy stage. A stage whose calls take most of the pool’s time leaves fewer threads for the other stages, and its calls queue behind theirs.

Examples from production pipelines

Several production training pipelines were migrated from their previous data loaders to SPDL loaders with node-local caching, each loader running its stages in worker processes. While bringing them to parity with the previous loaders, we measured four of them with their stage work on the worker’s shared pool and on dedicated pools, with all other optimizations applied.

Two of them illustrate the range of outcomes. The table compares 2 runs on the shared pool with 4 on dedicated pools for loader A, and with 3 for loader B:

  • Loader A does its whole per-batch work (a cache read, a transform and the collation) in one async stage, around an awaited fetch. The dedicated pool is a 1-thread executor for that stage.

  • Loader B has a heavy per-row transform and many threads per worker. The dedicated pools are separate executors for the read, transform and collation stages. It is the loader of the case study Debugging the kernel time of a cached loader, whose final configuration is the dedicated-pool column here.

Measure

Shared pool

Dedicated pool

Loader A

Epoch time

2,444 s (+9.6%)

2,229 s

Transform minor page faults per row

152.5

90.3

Transform kernel time per batch

0.24-0.26 s

0.17-0.20 s

Peak anonymous memory per host

935 GiB

877 GiB

Loader B

Warm epoch time

68.9 s

68.1 s

Minor page faults per row, all stages

54.3

21.8

Transform minor page faults per row

29.4

6.9

Kernel CPU per host

12.9 CPUs

11.7 CPUs

Peak anonymous memory per host

751 GiB (701-801)

583 GiB

Shared pool vs dedicated pool. Epoch time: loader A 2,444 vs 2,229 s (+9.6%), loader B 68.9 vs 68.1 s. Minor faults per row: loader A transform 152.5 vs 90.3, loader B all stages 54.3 vs 21.8, loader B transform 29.4 vs 6.9. Loader A transform kernel time per batch 0.24-0.26 vs 0.17-0.20 s. Loader B kernel CPU 12.9 vs 11.7 CPUs. Peak anonymous memory: loader A 935 vs 877 GiB, loader B 751 vs 583 GiB.

The table above as a figure. Epoch time is shown relative to the dedicated pool. Loader A’s kernel time is the range measured at epoch 6.

Notes on the measures:

  • Times and counters are means over the runs. Loader A’s epoch time is the time of epochs 2-6, and its faults and memory are means over those epochs. With two later dedicated-pool runs added (6 runs, 2,248 s), the shared pool is 8.7% slower. Its kernel-time range is from epoch 6, where its transform faults per row were 118-122 on the shared pool and 84-90 on dedicated pools. Loader B’s figures are from warm epochs; its 0.8 s difference is within the run-to-run spread, which comes from the training step rather than the loader.

  • Minor page faults were measured per op call (Advanced: measuring each op call), summed over workers and divided by rows; in one of loader B’s dedicated-pool runs, by thread name with SystemMonitor instead. Kernel time is ru_stime per batch for loader A, and the loader’s kernel CPUs for loader B. Peak memory is the per-host anonymous memory, as the mean over epochs (loader A) or the maximum over warm epochs (loader B); for loader B’s shared pool, the range of the two runs is in parentheses.

The other two loaders showed no measurable time difference.

The dedicated pool was needed for time only in loader A. In loader B, the shared pool cost page faults, kernel CPU and memory, not time.

A possible explanation for the extra faults is the memory allocator: on a shared pool, consecutive steps of one item usually run on different threads, so memory allocated on one thread is freed on another, and allocators keep per-thread caches. This is a hypothesis, not an established cause. Allocator effects that are known to raise faults are described in The allocator behind the counters.

How to measure

Measure before adopting a dedicated pool. Compare end-to-end time with and without it first, with at least two runs of each. Then compare CPU, faults and memory as described in Monitoring CPU, page faults, memory and pressure of a data loader: a stage with its own pool shows up under its thread_name_prefix in SystemMonitor (in region workers, and in the main process on Python 3.14 and later), and a stage on the shared pool needs the per-call measurement of Advanced: measuring each op call.

How to use it

Pass the executor to the stage:

from concurrent.futures import ThreadPoolExecutor

read_pool = ThreadPoolExecutor(max_workers=4, thread_name_prefix="read")

pipeline = (
    PipelineBuilder()
    .add_source(...)
    .to(ProcessPoolExecutorConfig(max_workers=8))
    .pipe(read, concurrency=4, executor=read_pool)
    .pipe(transform, concurrency=2)
    .to(MAIN_PROCESS)
    .add_sink(...)
    .build(num_threads=...)
)
  • In the main process, the stage runs on that executor. You own it and shut it down after the pipeline.

  • Inside a .to() region, each worker builds its own pool with the same max_workers and thread_name_prefix and shuts it down when the worker exits. Stages given the same executor object share one pool per worker. A stage with its own executor does not count toward the size of the worker’s shared pool. The executor must be freshly constructed; process pools and other executor types are rejected when the pipeline is built.

  • Size the pool to the number of calls the stage runs at once (usually its concurrency).

Async stages

An async op runs as a coroutine on the event loop, so its synchronous work goes to an executor. Give the stage one and offload that work with run_in_stage_executor():

from spdl.pipeline import run_in_stage_executor

async def load(key):
    data = await fetch(key)  # async I/O on the event loop
    return await run_in_stage_executor(decode_and_collate, data)

.pipe(load, concurrency=4, executor=ThreadPoolExecutor(1, thread_name_prefix="batch"))

The same rules apply inside regions. concurrency still bounds the items in flight; the pool’s max_workers bounds the synchronous calls. Loader A in the production examples above uses this layout.

What it costs

  • Threads. Each distinct executor adds max_workers threads to every worker process that uses it.

  • Throughput if too narrow. A pool narrower than the stage’s concurrency serializes the stage’s calls. It also slows failure reporting in a region: after a failure, the pipeline waits for each worker to finish the items it already has, and a narrow pool drains them one at a time.

  • Memory. Each thread has its own stack and allocator state. Report resident memory next to time and kernel CPU when you compare with and without the pool.