Datatoy Logo

Stateful workers

One ML model per worker, loaded once

`.class_task(MyClass, MyClass.method, args)` instantiates `MyClass` once per worker. The instance persists across items — perfect for ML models, DB connections, accumulators.

python
from olympipe import Pipeline
from transformers import pipeline as hf_pipeline

class EmbeddingWorker:
    def __init__(self, model_name: str):
        # Chargé une fois au démarrage du worker
        self.model = hf_pipeline("feature-extraction", model=model_name)

    def embed(self, text: str) -> list:
        return self.model(text)[0][0]  # CLS token

# 2 workers GPU, chacun charge le modèle une fois
embeddings = (
    Pipeline(texts)          # 50 000 textes
    .batch(32)
    .class_task(
        EmbeddingWorker,
        EmbeddingWorker.embed,
        ["sentence-transformers/all-MiniLM-L6-v2"],
        count=2,
    )
    .wait_for_result()
)

Performance

50 000 texts, HuggingFace embeddings

Sequential 2.0min
1 GPU worker 40.0s
2 GPU workers 21.0s
4 GPU workers 11.0s
🚀 10.9× faster

How it works

When the worker process starts, the constructor is called with `args`. All subsequent invocations reuse the same instance — model weights stay in RAM (or VRAM) without reloading.

Related examples