Chapter 2. The Ray API

To be intuitive, Ray must leverage concepts you already know. It does this by building on two familiar concepts in Python (and most languages): functions for stateless computing and classes for stateful computing.

This chapter offers a taste of the core Ray API. For more details and longer examples, see my Ray class on the O’Reilly Learning Platform. See the Ray documentation for extensive API details.

If you aren’t interested in the details of the core Ray API, skip ahead to the next chapter.

Just Six API Methods

Table 2-1 illustrates how almost everything you do with Ray is done with just six API methods. For a description of actors, see “What Are Actors?”.

There are other API methods for various administrative and informational purposes that we won’t go into. These six API methods are the essence of Ray, which provide its concision, flexibility, and power.

Table 2-1. The magnificent six
API method Description Example

ray.init()

Initialize Ray in your application.

ray.init()

@ray.remote

Decorate a function to make it a remote task. Decorate a class to make it a remote actor.

@ray.remote def train_model(source): … @ray.remote class ActivityTracker(): def record(event): … return count

.remote()

Construct an actor instance or asynchronously run a task or an actor method.

m_ref = train_model.remote(…) actor = ActivityTracker.remote() r_ref = tracker.record.remote(…)

ray.put()

Put a value in the distributed object store.

obj_ref = ray.put(my_object) ...

Get What Is Ray? now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.