spdl.pipeline.BackgroundTask

class BackgroundTask[source]

A background task that runs alongside pipeline stages.

Subclass this and override run() to implement custom logic. The task is started when the pipeline starts and cancelled when the pipeline completes. Errors are logged but do not cause the pipeline to fail.

Example:

class MyMonitor(BackgroundTask):
    async def run(self) -> None:
        while True:
            collect_metrics()
            await asyncio.sleep(60)

pipeline = build_pipeline(cfg, num_threads=4,
                          background_tasks=[MyMonitor])

Methods

run()

Override this to implement the background task logic.

async run() None[source]

Override this to implement the background task logic.

This coroutine runs in the pipeline’s event loop. It will be cancelled when the pipeline completes, so use try/except asyncio.CancelledError if cleanup is needed.