Ray Core for Python Systems

Back to modules
Course progress50%
article

Task and actor mental model

Choose the right Ray primitive for parallel Python work.

Ray task and actor mental model

Ray gives Python teams two core building blocks: stateless tasks and stateful actors. The design goal is to keep local Python ergonomics while moving execution across cores and machines.

Tasks for parallel functions

Use a task when the work can be expressed as a function call with inputs and an output. Ray returns an object reference immediately, which lets the driver submit more work before collecting results.

import ray

ray.init()

@ray.remote
def score_batch(batch):
    return [model_score(row) for row in batch]

refs = [score_batch.remote(batch) for batch in batches]
scores = ray.get(refs)

Actors for stateful services

Use an actor when the worker needs memory across calls: model weights, caches, counters, external connections, or streaming state.

@ray.remote
class FeatureCache:
    def __init__(self):
        self.values = {}

    def put(self, key, value):
        self.values[key] = value

    def get(self, key):
        return self.values.get(key)

Operational checklist

  • Keep task arguments small and read large data from shared storage.
  • Batch fine-grained work before sending it to the cluster.
  • Use actors for expensive initialization and repeated calls.
  • Prefer explicit resource requests when workloads need GPUs or high-memory nodes.

Task and actor mental model

Core foundations