3

I'm trying to find an elegant way to make sure a Ray actor gets instantiated only one time (like a singleton) so that if someone calls Singleton.remote() the already launched actor is returned. Is that possible? The common singleton decorator approach does not work.

Unziello
  • 103
  • 8

1 Answers1

0

It looks like the way to handle this within the default capability of ray core is to leverage the named actors functionality: https://docs.ray.io/en/latest/ray-core/actors/named-actors.html An example from the ray.io docs:

import ray

@ray.remote
class Actor:
  def say_hello(self, message):
      print(message)

# driver_1.py
# Job 1 creates an actor, "orange" in the "colors" namespace.
ray.init(address="auto", namespace="colors")
Actor.options(name="orange", lifetime="detached").remote()

# Actor `yellow` doesn't yet exist, so it is created with the given args.
a = Color.options(name="yellow", get_if_exists=True).remote("I'm Yellow")
assert ray.get(a.say_hello.remote()) == "I'm Yellow"

# Actor `green` already exists, so it is returned (new args are ignored).
b = Greeter.options(name="yellow", get_if_exists=True).remote("I'm Green")
assert ray.get(b.say_hello.remote()) == "Old Greeting"
OpenCoderX
  • 6,180
  • 7
  • 34
  • 61