使用命名空间#

命名空间是作业和命名角色的逻辑分组。当一个角色被命名时,其名称在命名空间内必须是唯一的。

为了设置应用程序的命名空间,应在首次连接到集群时指定。

import ray

ray.init(namespace="hello")
System.setProperty("ray.job.namespace", "hello"); // set it before Ray.init()
Ray.init();
ray::RayConfig config;
config.ray_namespace = "hello";
ray::Init(config);

请参考 驱动选项 以了解配置Java应用程序的方法。

命名角色仅在其命名空间内可访问。

import subprocess
import ray

try:
    subprocess.check_output(["ray", "start", "--head"])

    @ray.remote
    class Actor:
      pass

    # Job 1 creates two actors, "orange" and "purple" in the "colors" namespace.
    with ray.init("ray://localhost:10001", namespace="colors"):
      Actor.options(name="orange", lifetime="detached").remote()
      Actor.options(name="purple", lifetime="detached").remote()

    # Job 2 is now connecting to a different namespace.
    with ray.init("ray://localhost:10001", namespace="fruits"):
      # This fails because "orange" was defined in the "colors" namespace.
      try:
        ray.get_actor("orange")
      except ValueError:
        pass

      # This succceeds because the name "orange" is unused in this namespace.
      Actor.options(name="orange", lifetime="detached").remote()
      Actor.options(name="watermelon", lifetime="detached").remote()

    # Job 3 connects to the original "colors" namespace
    context = ray.init("ray://localhost:10001", namespace="colors")

    # This fails because "watermelon" was in the fruits namespace.
    try:
      ray.get_actor("watermelon")
    except ValueError:
      pass

    # This returns the "orange" actor we created in the first job, not the second.
    ray.get_actor("orange")

    # We are manually managing the scope of the connection in this example.
    context.disconnect()
finally:
    subprocess.check_output(["ray", "stop", "--force"])
// `ray start --head` has been run to launch a local cluster.

// Job 1 creates two actors, "orange" and "purple" in the "colors" namespace.
System.setProperty("ray.address", "localhost:10001");
System.setProperty("ray.job.namespace", "colors");
try {
    Ray.init();
    Ray.actor(Actor::new).setName("orange").remote();
    Ray.actor(Actor::new).setName("purple").remote();
} finally {
    Ray.shutdown();
}

// Job 2 is now connecting to a different namespace.
System.setProperty("ray.address", "localhost:10001");
System.setProperty("ray.job.namespace", "fruits");
try {
    Ray.init();
    // This fails because "orange" was defined in the "colors" namespace.
    Ray.getActor("orange").isPresent(); // return false
    // This succceeds because the name "orange" is unused in this namespace.
    Ray.actor(Actor::new).setName("orange").remote();
    Ray.actor(Actor::new).setName("watermelon").remote();
} finally {
    Ray.shutdown();
}

// Job 3 connects to the original "colors" namespace.
System.setProperty("ray.address", "localhost:10001");
System.setProperty("ray.job.namespace", "colors");
try {
    Ray.init();
    // This fails because "watermelon" was in the fruits namespace.
    Ray.getActor("watermelon").isPresent(); // return false
    // This returns the "orange" actor we created in the first job, not the second.
    Ray.getActor("orange").isPresent(); // return true
} finally {
    Ray.shutdown();
}
// `ray start --head` has been run to launch a local cluster.

// Job 1 creates two actors, "orange" and "purple" in the "colors" namespace.
ray::RayConfig config;
config.ray_namespace = "colors";
ray::Init(config);
ray::Actor(RAY_FUNC(Counter::FactoryCreate)).SetName("orange").Remote();
ray::Actor(RAY_FUNC(Counter::FactoryCreate)).SetName("purple").Remote();
ray::Shutdown();

// Job 2 is now connecting to a different namespace.
ray::RayConfig config;
config.ray_namespace = "fruits";
ray::Init(config);
// This fails because "orange" was defined in the "colors" namespace.
ray::GetActor<Counter>("orange"); // return nullptr;
// This succeeds because the name "orange" is unused in this namespace.
ray::Actor(RAY_FUNC(Counter::FactoryCreate)).SetName("orange").Remote();
ray::Actor(RAY_FUNC(Counter::FactoryCreate)).SetName("watermelon").Remote();
ray::Shutdown();

// Job 3 connects to the original "colors" namespace.
ray::RayConfig config;
config.ray_namespace = "colors";
ray::Init(config);
// This fails because "watermelon" was in the fruits namespace.
ray::GetActor<Counter>("watermelon"); // return nullptr;
// This returns the "orange" actor we created in the first job, not the second.
ray::GetActor<Counter>("orange");
ray::Shutdown();

为命名角色指定命名空间#

在创建命名角色时,您可以为其指定一个命名空间。无论当前作业的命名空间是什么,创建的角色都将属于指定的命名空间。

import subprocess
import ray

try:
    subprocess.check_output(["ray", "start", "--head"])

    @ray.remote
    class Actor:
        pass

    ctx = ray.init("ray://localhost:10001")

    # Create an actor with specified namespace.
    Actor.options(name="my_actor", namespace="actor_namespace", lifetime="detached").remote()

    # It is accessible in its namespace.
    ray.get_actor("my_actor", namespace="actor_namespace")
    ctx.disconnect()
finally:
    subprocess.check_output(["ray", "stop", "--force"])
// `ray start --head` has been run to launch a local cluster.

System.setProperty("ray.address", "localhost:10001");
try {
    Ray.init();
    // Create an actor with specified namespace.
    Ray.actor(Actor::new).setName("my_actor", "actor_namespace").remote();
    // It is accessible in its namespace.
    Ray.getActor("my_actor", "actor_namespace").isPresent(); // return true

} finally {
    Ray.shutdown();
}
// `ray start --head` has been run to launch a local cluster.
ray::RayConfig config;
ray::Init(config);
// Create an actor with specified namespace.
ray::Actor(RAY_FUNC(Counter::FactoryCreate)).SetName("my_actor", "actor_namespace").Remote();
// It is accessible in its namespace.
ray::GetActor<Counter>("orange");
ray::Shutdown();`

匿名命名空间#

当未指定命名空间时,Ray 会将您的作业放置在匿名命名空间中。在匿名命名空间中,您的作业将拥有自己的命名空间,并且无法访问其他命名空间中的角色。

import subprocess
import ray

try:
  subprocess.check_output(["ray", "start", "--head"])

  @ray.remote
  class Actor:
      pass

  # Job 1 connects to an anonymous namespace by default
  with ray.init("ray://localhost:10001"):
    Actor.options(name="my_actor", lifetime="detached").remote()

  # Job 2 connects to a _different_ anonymous namespace by default
  with ray.init("ray://localhost:10001"):
    # This succeeds because the second job is in its own namespace.
    Actor.options(name="my_actor", lifetime="detached").remote()

finally:
    subprocess.check_output(["ray", "stop", "--force"])
// `ray start --head` has been run to launch a local cluster.

// Job 1 connects to an anonymous namespace by default.
System.setProperty("ray.address", "localhost:10001");
try {
    Ray.init();
    Ray.actor(Actor::new).setName("my_actor").remote();
} finally {
    Ray.shutdown();
}

// Job 2 connects to a _different_ anonymous namespace by default
System.setProperty("ray.address", "localhost:10001");
try {
    Ray.init();
    // This succeeds because the second job is in its own namespace.
    Ray.actor(Actor::new).setName("my_actor").remote();
} finally {
    Ray.shutdown();
}
// `ray start --head` has been run to launch a local cluster.

// Job 1 connects to an anonymous namespace by default.
ray::RayConfig config;
ray::Init(config);
ray::Actor(RAY_FUNC(Counter::FactoryCreate)).SetName("my_actor").Remote();
ray::Shutdown();

// Job 2 connects to a _different_ anonymous namespace by default
ray::RayConfig config;
ray::Init(config);
// This succeeds because the second job is in its own namespace.
ray::Actor(RAY_FUNC(Counter::FactoryCreate)).SetName("my_actor").Remote();
ray::Shutdown();

备注

匿名命名空间实现为UUID。这使得未来的作业可以手动连接到现有的匿名命名空间,但不推荐这样做。

获取当前命名空间#

你可以使用 运行时上下文API 访问当前命名空间。

import subprocess
import ray

try:
  subprocess.check_output(["ray", "start", "--head"])

  ray.init(address="auto", namespace="colors")

  # Will print namespace name "colors".
  print(ray.get_runtime_context().namespace)

finally:
    subprocess.check_output(["ray", "stop", "--force"])
System.setProperty("ray.job.namespace", "colors");
try {
    Ray.init();
    // Will print namespace name "colors".
    System.out.println(Ray.getRuntimeContext().getNamespace());
} finally {
    Ray.shutdown();
}
ray::RayConfig config;
config.ray_namespace = "colors";
ray::Init(config);
// Will print namespace name "colors".
std::cout << ray::GetNamespace() << std::endl;
ray::Shutdown();