# Obtain actor-handle from within an actor to self

**URL:** <https://discuss.ray.io/t/obtain-actor-handle-from-within-an-actor-to-self/10257>\
**Category:** Ray Core\
**Created:** [April 17, 2023, 7:57am UTC](https://discuss.ray.io/t/obtain-actor-handle-from-within-an-actor-to-self/10257 "2023-04-17T07:57:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eladgm](https://avatars.discourse-cdn.com/v4/letter/e/ec9cab/32.png) [@eladgm](https://discuss.ray.io/u/eladgm)\
**Post date:** [April 17, 2023, 7:57am UTC](https://discuss.ray.io/t/obtain-actor-handle-from-within-an-actor-to-self/10257/1 "2023-04-17T07:57:16Z")

</div>

From within a ray actor I would like to obtain a handle to my own actor and pass it via remote calls. This is necessary for implementing a flow that follows the “observer-pattern”. In such flow, actor “C” invokes a remote call to “P” of the form `P_handle.subscribe.remote(C_handle)` to register itself as an “observer” (consumer).

How can actor “C” obtain `C_handle` in such case?

Thanks,  
Elad

---

<div class="post-metadata">

**Author:** ![rickyyx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/rickyyx/32/2671_2.png) [@rickyyx](https://discuss.ray.io/u/rickyyx)\
**Post date:** [April 19, 2023, 8:56pm UTC](https://discuss.ray.io/t/obtain-actor-handle-from-within-an-actor-to-self/10257/2 "2023-04-19T20:56:33Z")

</div>

Hey @eladgm - Thanks for the question.

~~AFAIK, there’s no current API for doing so.~~

However, when you create the actor, you will get the actor handle, and you should be able to pass the handle as an argument to the actor method.

Assuming you have an actor method that calls

```python
@ray.remote
class Actor:
    def subscribe(self, C_ref, P_ref):
        P_ref.subscribe.remote(C_ref)

```

And you pass the actor ref at the caller side:

```python
c = Actor.remote()
p = P.remote()
c.subscribe.remote(c, p)

```

Will this work for your usecase?

---

<div class="post-metadata">

**Author:** ![rickyyx](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/rickyyx/32/2671_2.png) [@rickyyx](https://discuss.ray.io/u/rickyyx)\
**Post date:** [April 19, 2023, 11:31pm UTC](https://discuss.ray.io/t/obtain-actor-handle-from-within-an-actor-to-self/10257/3 "2023-04-19T23:31:05Z")

</div>

You could actually do so by (in your actor method)

```auto

ctx = ray.get_runtime_context()
actor_handle = ctx.current_actor()

```

[https://docs.ray.io/en/latest/ray-core/api/doc/ray.runtime\_context.RuntimeContext.html?highlight=current\_actor](https://docs.ray.io/en/latest/ray-core/api/doc/ray.runtime_context.RuntimeContext.html?highlight=current_actor)  
and [ray.runtime\_context.get\_runtime\_context — Ray 2.3.1](https://docs.ray.io/en/latest/ray-core/api/doc/ray.runtime_context.get_runtime_context.html)

---

<div class="post-metadata">

**Author:** ![eladgm](https://avatars.discourse-cdn.com/v4/letter/e/ec9cab/32.png) [@eladgm](https://discuss.ray.io/u/eladgm)\
**Post date:** [April 20, 2023, 7:34am UTC](https://discuss.ray.io/t/obtain-actor-handle-from-within-an-actor-to-self/10257/4 "2023-04-20T07:34:12Z")

</div>

Thanks! Great. That is exactly what I was looking for. The following code works for me for checking if the current execution is within an actor, and if so obtain a handle to it:

```python
def get_handle_to_current_actor() -> Optional[ray.actor.ActorHandle]:
    """
    Obtain an handle to current actor if executed within one.
    """
    ctx = ray.get_runtime_context()
    if not ctx.worker.mode:
        return None # not inside of an actor
    return ctx.current_actor

```
