Fundamentals 8 min read

Mastering Ray: Core Concepts of Tasks, Actors, and Objects for Distributed Computing

This guide explains Ray's fundamental building blocks—including Tasks, Actors, remote Objects, Placement Groups, and environment dependencies—showing how to define, schedule, and retrieve distributed workloads with code examples and command‑line utilities.

Big Data Technology Tribe
Big Data Technology Tribe
Big Data Technology Tribe
Mastering Ray: Core Concepts of Tasks, Actors, and Objects for Distributed Computing

Tasks

Ray allows any Python function to be executed asynchronously on separate worker processes. Each asynchronous execution is a Task . Tasks can declare resource requirements (CPU, GPU, custom resources) which the Ray scheduler uses to place them for parallel execution.

import ray
import time

# Regular Python function
def normal_function():
    return 1

# Remote function – the @ray.remote decorator turns it into a Ray task
@ray.remote
def my_function():
    return 1

# Invoke the remote function; this returns an ObjectRef immediately
obj_ref = my_function.remote()
assert ray.get(obj_ref) == 1

# Example of a longer‑running task
@ray.remote
def slow_function():
    time.sleep(10)
    return 1

# Launch several tasks in parallel (non‑blocking)
for _ in range(4):
    slow_function.remote()

The CLI command ray summary tasks prints a table of active and completed tasks, showing counts per function and their current state.

Actors

Actors extend the Ray API from functions to stateful classes. Each actor runs in its own process, preserving internal state across method calls. Like tasks, actors can request specific resources.

import ray

@ray.remote
class Counter:
    def __init__(self):
        self.value = 0
    def increment(self):
        self.value += 1
        return self.value
    def get_counter(self):
        return self.value

# Create an actor instance; this spawns a dedicated worker process
counter = Counter.remote()

The CLI command ray list actors lists active actors, displaying their IDs, class names, state (e.g., ALIVE), job IDs, node IDs and process IDs.

Remote Objects

Both tasks and actors produce remote objects that are stored in Ray's distributed shared‑memory object store. An ObjectRef acts like a future or pointer; it can be passed around without moving the underlying data.

import ray
import time
from ray.exceptions import GetTimeoutError

# Put a value directly into the object store
y = 1
object_ref = ray.put(y)
assert ray.get(object_ref) == 1

# Retrieve multiple objects in parallel
assert ray.get([ray.put(i) for i in range(3)]) == [0, 1, 2]

# Example with a timeout
@ray.remote
def long_running_function():
    time.sleep(8)

obj_ref = long_running_function.remote()
try:
    ray.get(obj_ref, timeout=4)
except GetTimeoutError:
    print("`get` timed out.")

Object references are immutable, automatically reference‑counted, and for NumPy arrays they provide zero‑copy access by returning a shared‑memory view instead of a deserialized copy.

Placement Groups

Placement Groups let users atomically reserve a set of resources across multiple nodes. They support two packing strategies: PACK (tight locality) and SPREAD (distributed). Placement groups are useful for batch scheduling of actors or tasks that need co‑located resources.

For detailed usage see: https://docs.ray.io/en/releases-2.51.1/ray-core/scheduling/placement-group.html

Environment Dependencies

When Ray runs tasks or actors on remote machines, the required Python packages, files, and environment variables must be present on those machines. This can be handled by pre‑installing dependencies with the Ray Cluster Launcher or by using Ray's runtime environments feature to install them dynamically at execution time.

Reference documentation: https://docs.ray.io/en/releases-2.51.1/index.html#

Distributed ComputingRayActorsObject StoreTasks
Big Data Technology Tribe
Written by

Big Data Technology Tribe

Focused on computer science and cutting‑edge tech, we distill complex knowledge into clear, actionable insights. We track tech evolution, share industry trends and deep analysis, helping you keep learning, boost your technical edge, and ride the digital wave forward.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.