命名 Actor#
Actor 在其命名空间内可以被赋予一个唯一的名称。这允许你从 Ray 集群中的任何作业中检索该 Actor。如果你无法直接将 Actor handle 传递给需要它的任务,或者你试图访问由另一个 driver 启动的 Actor,这会很有用。请注意,如果不存在指向该 Actor 的 handle,该 Actor 仍会被垃圾回收。更多详情请参阅Actor 生命周期。
import ray
@ray.remote
class Counter:
pass
# Create an actor with a name
counter = Counter.options(name="some_name").remote()
# Retrieve the actor later somewhere
counter = ray.get_actor("some_name")
// Create an actor with a name.
ActorHandle<Counter> counter = Ray.actor(Counter::new).setName("some_name").remote();
...
// Retrieve the actor later somewhere
Optional<ActorHandle<Counter>> counter = Ray.getActor("some_name");
Assert.assertTrue(counter.isPresent());
// Create an actor with a globally unique name
ActorHandle<Counter> counter = ray::Actor(CreateCounter).SetGlobalName("some_name").Remote();
...
// Retrieve the actor later somewhere
boost::optional<ray::ActorHandle<Counter>> counter = ray::GetGlobalActor("some_name");
我们还在 C++ 中支持非全局命名 Actor,这意味着 Actor 名称仅在作业内有效,不能从其他作业访问该 Actor。
// Create an actor with a job-scope-unique name
ActorHandle<Counter> counter = ray::Actor(CreateCounter).SetName("some_name").Remote();
...
// Retrieve the actor later somewhere in the same job
boost::optional<ray::ActorHandle<Counter>> counter = ray::GetActor("some_name");
注意
命名 Actor 通过命名空间限定范围。如果未指定命名空间,它们将默认放置在匿名命名空间中。
import ray
@ray.remote
class Actor:
pass
# 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()
# driver_2.py
# Job 2 is now connecting to a different namespace.
ray.init(address="auto", namespace="fruit")
# This fails because "orange" was defined in the "colors" namespace.
ray.get_actor("orange")
# You can also specify the namespace explicitly.
ray.get_actor("orange", namespace="colors")
# driver_3.py
# Job 3 connects to the original "colors" namespace
ray.init(address="auto", namespace="colors")
# This returns the "orange" actor we created in the first job.
ray.get_actor("orange")
import ray
class Actor {
}
// Driver1.java
// Job 1 creates an actor, "orange" in the "colors" namespace.
System.setProperty("ray.job.namespace", "colors");
Ray.init();
Ray.actor(Actor::new).setName("orange").remote();
// Driver2.java
// Job 2 is now connecting to a different namespace.
System.setProperty("ray.job.namespace", "fruits");
Ray.init();
// This fails because "orange" was defined in the "colors" namespace.
Optional<ActorHandle<Actor>> actor = Ray.getActor("orange");
Assert.assertFalse(actor.isPresent()); // actor.isPresent() is false.
// Driver3.java
System.setProperty("ray.job.namespace", "colors");
Ray.init();
// This returns the "orange" actor we created in the first job.
Optional<ActorHandle<Actor>> actor = Ray.getActor("orange");
Assert.assertTrue(actor.isPresent()); // actor.isPresent() is true.
获取或创建命名 Actor#
一个常见的用例是仅当 Actor 不存在时才创建它。Ray 为 Actor 创建提供了一个开箱即用的 get_if_exists
选项。在通过 .options()
为 Actor 设置名称后,此方法可用。
如果 Actor 已存在,将返回该 Actor 的 handle 并忽略参数。否则,将使用指定的参数创建一个新的 Actor。
import ray
@ray.remote
class Greeter:
def __init__(self, value):
self.value = value
def say_hello(self):
return self.value
# Actor `g1` doesn't yet exist, so it is created with the given args.
a = Greeter.options(name="g1", get_if_exists=True).remote("Old Greeting")
assert ray.get(a.say_hello.remote()) == "Old Greeting"
# Actor `g1` already exists, so it is returned (new args are ignored).
b = Greeter.options(name="g1", get_if_exists=True).remote("New Greeting")
assert ray.get(b.say_hello.remote()) == "Old Greeting"
// This feature is not yet available in Java.
// This feature is not yet available in C++.
Actor 生命周期#
另外,Actor 的生命周期可以与作业解耦,允许 Actor 在作业的 driver 进程退出后仍然存在。我们将这些 Actor 称为分离的 (detached)。
counter = Counter.options(name="CounterActor", lifetime="detached").remote()
即使运行上述脚本的 driver 退出后,CounterActor
仍会保持活动。因此,可以在不同的 driver 中运行以下脚本。
counter = ray.get_actor("CounterActor")
请注意,Actor 可以被命名但不分离。如果我们只指定了名称但没有指定 lifetime="detached"
,那么只要原始 driver 仍在运行,就可以检索到 CounterActor。
System.setProperty("ray.job.namespace", "lifetime");
Ray.init();
ActorHandle<Counter> counter = Ray.actor(Counter::new).setName("some_name").setLifetime(ActorLifetime.DETACHED).remote();
即使运行上述进程的 driver 退出后,CounterActor 仍会保持活动。因此,可以在不同的 driver 中运行以下代码。
System.setProperty("ray.job.namespace", "lifetime");
Ray.init();
Optional<ActorHandle<Counter>> counter = Ray.getActor("some_name");
Assert.assertTrue(counter.isPresent());
Actor 生命周期自定义功能尚未在 C++ 中实现。
与普通 Actor 不同,分离的 Actor 不会被 Ray 自动垃圾回收。一旦你确定不再需要分离的 Actor,必须手动销毁它们。为此,使用 ray.kill
来手动终止 Actor。调用此方法后,可以重复使用 Actor 的名称。