Broadcast data to ray actors in an elegant way

Hi, I defined an Actor class:

@ray.remote
class A:
    def __init__(self):
        self.a = 1
    def run(self):
        while True:
            # do something with self.a

Then I created many actors,

actors = [A() for _ in range(10)]
ws = [a.run.remote() for a in actors] # run the background

In the main process, I want to broadcast the values of a to these actors. I think I can define a function called set_a in the class of A, but I think that is not very efficient. I can also use a queue for each actor to send data to all actors. However, I think that is trivial because I should create many queues. Is there any elegant way to achieve this?

Assuming a is large, an efficient way to broadcast is to use the Ray object store. You can do:

ref = ray.put(data_for_a)
ray.get([a.set_a.remote(ref) for a in actors])

Internally, Ray will broadcast the data efficiently via the object store. There is some overhead of calling the method, but it’s fairly small compared to data transfer.

1 Like

@ericl Hi, thanks for the reply.