spdl.pipeline.run_in_stage_executor

async run_in_stage_executor(func: Callable[[...], T], /, *args: Any, **kwargs: Any) → T[source]

Run a synchronous function on the executor of the current async stage.

An async op given an executor in pipe() (or Pipe()) is still awaited on the pipeline’s event loop. The executor is where the op offloads its synchronous work: calling this function from the op runs func(*args, **kwargs) on that executor and awaits the result. Inside a to() worker region, it runs on the pool that each worker builds from the stage’s executor.

When the current stage has no executor, or when called outside a pipeline stage, func runs on the running event loop’s default executor, as loop.run_in_executor(None, ...) does. In a pipeline, that is the pipeline’s thread pool (sized by num_threads, or inside a region by the region’s synchronous stages).

Example

from concurrent.futures import ThreadPoolExecutor

from spdl.pipeline import PipelineBuilder, 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, data)  # sync work


pipeline = (
    PipelineBuilder()
    .add_source(keys)
    .pipe(load, concurrency=16, executor=ThreadPoolExecutor(4))
    .add_sink(4)
    .build(num_threads=4)
)
Parameters:
  • func – The synchronous function to call.

  • args – The arguments to pass to func.

  • kwargs – The arguments to pass to func.

Returns:

The return value of func.

Raises:

Exception – Whatever func raises.

Added in version 0.9.0.