Actor accessing its own actor handle

  • Low: It annoys or frustrates me for a moment.

Hi everyone,
I would like to know if it’s possible for an actor to access its own actor handle ?

I am interested in this for a use case where actor A calls a remote function of actor B and would like to pass its own actor handle through this call in order for B to be able to reply to him with another remote function call.

Here is an example where some unknown function “GET_SELF_ACTOR_HANDLE” would do what I am looking for:

@ray.remote
class A:
    def f(self, b):
        time.sleep(1)
        b.g.remote()

@ray.remote
class B:
    def __init__(self):
        self.x = 0
    
    def f(self, a):
        a.f.remote(GET_SELF_ACTOR_HANDLE(self))
        self.x = 5

    def g(self):
        print(self.x)

a = A.remote()
b = B.remote()
b.f.remote(a)

I understand that in this example I can add a “set_own_actor_handle” method like this:

@ray.remote
class A:
    def f(self, b):
        time.sleep(1)
        b.g.remote()

@ray.remote
class B:
    def __init__(self):
        self.x = 0

    def set_own_actor_handle(self, handle):
        self.handle = handle
    
    def f(self, a):
        a.f.remote(self.handle)
        self.x = 5

    def g(self):
        print(self.x)

a = A.remote()
b = B.remote()
ray.get(b.set_own_actor_handle.remote(b))
b.f.remote(a)

But I would like to know if something more elegant is possible.

Thank you

Hey @ai3el , check out ray.runtime_context.get_runtime_context().current_actor: Ray Core API — Ray 1.12.0

Many thanks for your answer @ericl

As a genuine advise for the developers, it’s not obvious to find in the docs when you’re new to ray. Typing “current actor handle” in the search bar doesn’t shows the right page whereas it’s the exact description of the function. Maybe this page is not referenced ?

Thanks again for your answer

1 Like