Ray Distributed Framework

2.54.1 · active · verified Sat Mar 28

Ray is a unified open-source framework for building and scaling distributed applications and AI workloads in Python. It provides simple APIs for parallelizing Python functions and classes (tasks and actors) and a toolkit of specialized libraries (Ray Data, Train, Tune, Serve, RLlib) for machine learning. Ray offers a universal compute layer for orchestrating clusters, scheduling processes, fault tolerance, and autoscaling. The project maintains a very frequent release cadence, with minor and patch releases occurring every few weeks.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic Ray Core usage, including initializing Ray, defining remote functions (tasks) using the `@ray.remote` decorator, launching these tasks with `.remote()`, and retrieving results with `ray.get()`. It includes an example of parallel execution with simulated delay.

import ray
import time
import os

# Initialize Ray (connects to an existing cluster or starts a local one)
# For a cluster, you might use ray.init(address="auto") or specify an address.
# For local testing, ray.init() is sufficient.
ray.init(address=os.environ.get('RAY_ADDRESS', None), ignore_reinit_error=True)

@ray.remote
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci.remote(n - 1) + fibonacci.remote(n - 2)

@ray.remote
def slow_square(x):
    time.sleep(1) # Simulate a slow computation
    return x * x

if __name__ == '__main__':
    print("--- Ray Tasks Example ---")
    # Run tasks in parallel
    futures = [slow_square.remote(i) for i in range(5)]
    results = ray.get(futures)
    print(f"Results from slow_square: {results}")

    # Example of recursive Ray tasks (note: fibonacci is a common but inefficient example for Ray's overhead)
    # For larger N, this can quickly create too many tasks.
    # result_fib = ray.get(fibonacci.remote(10))
    # print(f"Fibonacci(10) using Ray: {result_fib}")

    print("Ray initialized successfully, dashboard at:", ray.get_dashboard_url())

    ray.shutdown()
    print("Ray shutdown.")

view raw JSON →