Ray is an open-source Python framework for distributed computing that provides a unified runtime for building and scaling AI and ML workloads across laptops, clusters, and cloud platforms. It solves the core pain point of distributed systems complexity by exposing three simple primitives β tasks, actors, and objects β that map naturally to any Python program. The key mental model is that Ray is not a framework you adopt wholesale but a layer you add incrementally: a single decorator turns a regular function into a distributed task, and the rest of your code stays unchanged.
What This Cheat Sheet Covers
This topic spans 13 focused tables and 128 indexed concepts, 109 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Ray Core β Tasks
Tasks are the fundamental unit of stateless parallelism in Ray. Decorating a Python function with @ray.remote registers it as a remote function that executes asynchronously on any available worker in the cluster; results are retrieved lazily via ray.get(), enabling you to fire off thousands of tasks without blocking.
| Technique | Example | Description | |
|---|---|---|---|
def add(x, y): return x + yref = add.remote(1, 2) | β’ Decorates a Python function to run as a distributed remote task β’ .remote() returns an ObjectRef immediately without blocking | ||
result = ray.get(ref)results = ray.get([r1, r2, r3]) | β’ Blocks the caller and retrieves the concrete value(s) from one or more ObjectRefsβ’ accepts a single ref or a list | ||
ready, remaining = ray.wait( refs, num_returns=2, timeout=5.0) | β’ Returns two lists β refs that are ready and those still pending β without blocking indefinitely β’ use to process results as they finish rather than waiting for all | ||
ray.init()ray.init(address="ray://head:10001")ray.init(num_cpus=4) | β’ Starts a local Ray runtime or connects to an existing cluster β’ call once at the start of your program | ||
memory=2_000_000_000)def train(): ... | β’ Declares logical resources reserved for the task β’ Ray uses these for scheduling, not for enforcing physical limits | ||
def infer(): ... | β’ Reserves user-defined resource labels set at cluster startup β’ useful for special hardware like TPUs or FPGAs | ||
def light_infer(): ... | β’ Allows multiple tasks to share a single GPU β’ Ray supports fractions down to 0.0001 precision | ||
add.options(num_cpus=4).remote(x, y) | Overrides resource or metadata settings per invocation without redefining the function. | ||
def outer(): refs = [inner.remote(i) for i in range(4)] return ray.get(refs) | Tasks can themselves spawn further remote tasks, enabling hierarchical parallelism where work fans out across the cluster dynamically. | ||
def gen(): for i in range(5): yield ifor ref in gen.remote(): ... | A remote function can yield values instead of returning all at once, reducing peak heap memory for large outputs. |