Can the methods of a Ray actor be called in parallel on a single remote instance? it seems i have to create a new instance of the actor, then call the method on that instance in order to achieve parallelization. See the code and out put below
@ray.remote
class ActorTest:
def __init__(self) -> None:
self.factor = 10
def do_work(self, n):
result = self.factor * n
print(result)
return result
if __name__ == '__main__':
ray.init()
actor = ActorTest.remote()
results = [actor.do_work.remote(i) for i in range(10)]
print(ray.get(results))
This code will print out the following - notice that the process ids are all the same:
(ActorTest pid=15176) 10
(ActorTest pid=15176) 20
(ActorTest pid=15176) 30
(ActorTest pid=15176) 40
(ActorTest pid=15176) 50
(ActorTest pid=15176) 60
(ActorTest pid=15176) 70
(ActorTest pid=15176) 80
(ActorTest pid=15176) 90
Now if i were to change the code to where i create a new remote instance each time i call my task method, it works as i would expect. each call is executed in a separate process as can be seen from the output:
@ray.remote
class ActorTest:
def __init__(self) -> None:
self.factor = 10
def do_work(self, n):
result = self.factor * n
print(result)
return result
if __name__ == '__main__':
ray.init()
results = []
for i in range(10):
actor = ActorTest.remote()
results.append(actor.do_work.remote(i))
print(ray.get(results))
Here you see the results are printed out with a different process id:
(ActorTest pid=31464) 70
(ActorTest pid=26272) 0
(ActorTest pid=35320) 20
(ActorTest pid=30868) 60
(ActorTest pid=10696) 10
(ActorTest pid=15688) 30
(ActorTest pid=27984) 90
(ActorTest pid=39852) 40
(ActorTest pid=10380) 80