Anti-pattern: Using global variables to share state between tasks and actors
Anti-pattern: Using global variables to share state between tasks and actors#
TLDR: 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 are running in
different processes, so they don’t share the same address space.
This means that if you modify global variables
in one process, changes are not reflected in other processes.
The solution is to use an actor’s instance variables to hold the global state and pass the actor handle to places where the state needs to be modified or accessed.
Note that using class variables to manage state between instances of the same class is not supported.
Each actor instance is instantiated in its own process, so each actor will have its own copy of the class variables.
importrayray.init()email@example.comActor:deff(self):returnglobal_var+3actor=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.assertray.get(actor.f.remote())==6
@ray.remoteclassGlobalVarActor:def__init__(self):self.global_var=3defset_global_var(self,var):self.global_var=vardefget_global_var(self):firstname.lastname@example.orgActor:def__init__(self,global_var_actor):self.global_var_actor=global_var_actordeff(self):returnray.get(self.global_var_actor.get_global_var.remote())+3global_var_actor=GlobalVarActor.remote()actor=Actor.remote(global_var_actor)ray.get(global_var_actor.set_global_var.remote(4))# This returns 7 correctly.assertray.get(actor.f.remote())==7