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).
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:
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