APIs for creating and managing Ray actors (stateful remote classes).
Decorator to create an actor class.
@ray.remote
class MyActor:
def __init__(self, value):
self.value = value
def get_value(self):
return self.value
actor = MyActor.remote(42)Create an actor instance.
actor = MyActor.remote(*args, **kwargs)Returns an ActorHandle to the newly created actor.
Configure actor instantiation parameters.
Actor = MyActor.options(
num_cpus=2,
num_gpus=1,
resources={"CustomResource": 1},
max_restarts=3,
name="my_actor",
namespace="my_namespace",
lifetime="detached"
)
actor = Actor.remote()Key Parameters:
num_cpus,num_gpus: Resource requirementsresources: Custom resource dictionarymax_restarts: Maximum restart attempts (0=no restart, -1=infinite)max_task_retries: Retry failed tasks (0=no retry, -1=infinite)name: Globally unique actor name for retrievalnamespace: Actor namespacelifetime: "detached" for independent lifetime, None for fate-sharingruntime_env: Runtime environment configurationscheduling_strategy: Placement strategy
Annotate an actor method with options.
@ray.remote
class Actor:
@ray.method(num_returns=2, max_task_retries=3)
def method(self):
return 1, 2Parameters:
num_returns: Number of return values (default: 1, use "streaming" for generators)max_task_retries: Retry count for failed tasksretry_exceptions: Retry on Python exceptions (bool or list of exceptions)concurrency_group: Concurrency group name for the method
Configure method call options.
result = actor.method.options(num_returns=2).remote()A handle to an actor, returned by ActorClass.remote().
actor = MyActor.remote()
result = actor.method.remote()
value = ray.get(result)Actor handles can be passed to other tasks or actors.
Get a handle to a named actor.
actor = ray.get_actor("actor_name", namespace=None)The actor must have been created with Actor.options(name="actor_name").
Kill an actor forcefully.
ray.kill(actor, no_restart=True)Interrupts running tasks immediately. Use actor.__ray_terminate__.remote() to let pending tasks finish.
Exit the current actor from within the actor.
@ray.remote
class Actor:
def shutdown(self):
ray.actor.exit_actor()Can only be called from inside an actor.