Anti-pattern: Using global variables to share state between tasks and actors#

Don’t use global variables to share state with tasks and actors. Instead, encapsulate the global variables in an actor and pass the actor handle to other tasks and actors.

Ray drivers, tasks, and actors run in different processes, so they don’t share the same address space. If you modify global variables in one process, other processes don’t see the changes.

Hold the global state in an actor’s instance variables, and pass the actor handle to wherever your code reads or modifies the state. Ray doesn’t support using class variables to manage state between instances of the same class. Ray instantiates each actor instance in its own process, so each actor has its own copy of the class variables.

Code example#

Anti-pattern:

import ray

ray.init()

global_var = 3


@ray.remote
class Actor:
    def f(self):
        return global_var + 3


actor = Actor.remote()
global_var = 4
# This returns 6, not 7. It is because the value change of global_var
# inside a driver is not reflected to the actor
# because they are running in different processes.
assert ray.get(actor.f.remote()) == 6

Better approach:

@ray.remote
class GlobalVarActor:
    def __init__(self):
        self.global_var = 3

    def set_global_var(self, var):
        self.global_var = var

    def get_global_var(self):
        return self.global_var


@ray.remote
class Actor:
    def __init__(self, global_var_actor):
        self.global_var_actor = global_var_actor

    def f(self):
        return ray.get(self.global_var_actor.get_global_var.remote()) + 3


global_var_actor = GlobalVarActor.remote()
actor = Actor.remote(global_var_actor)
ray.get(global_var_actor.set_global_var.remote(4))
# This returns 7 correctly.
assert ray.get(actor.f.remote()) == 7