Specify port when using ray.init() to start new local instance

How severe does this issue affect your experience of using Ray?

  • High: It blocks me to complete my task.

In my service code, I am connecting to an existing ray cluster.

In local tests, I’m using ray.init() to start a local instance in a fixture and pass this as the head endpoint to the service code.

The problem is that using ray.init() to start a local instance opens a random port, so the endpoint always changes.

I do not see any options to specify the port for a new local instance.

Right now I am doing the following in a test fixture:

ray.init(local_mode=True, ignore_reinit_error=True, num_cpus=1)

But this fails because the service ray.init() tries to connect on a ray endpoint that is specified in the environment, does not find the local instance because the local port doesn’t match the environment, and times out.

So I want to do something like this in a test fixture:

RAY_PORT=10001 # Specified in the environment
ray.init(local_mode=True, ignore_reinit_error=True, num_cpus=1, port=RAY_PORT)

The goal is to then be able to connect on f"ray://127.0.0.1:{RAY_PORT}" using ray.init() in the code.

Thanks!

Hello! What version of Ray are you using? local_mode seems to be deprecated as of the latest version of Ray. local_mode also doesn’t really set up a Ray cluster (like you may have noticed) since it is more of a ephemeral process used for debugging.

Have you tried using the regular ray.init (docs here: ray.init — Ray 2.42.1) and setting variable dashboard_port to a specific port?

Here’s a guide on how to customize the dashboard port using ray.init() if it helps:

https://docs.ray.io/en/latest/cluster/configure-manage-dashboard.html

Thanks, @christina!

I’m using ray 2.34.0

The dashboard port isn’t the same as the head node port, which is what needs to be passed to ray.init(address=xxxx) in order to connect to an existing cluster. Setting the dashboard port doesn’t impact the issue I am describing.

Oh, I see what you mean! Ok, so this is a way to start Ray with a port via the CLI (ray start --head --port=6379) Starting Ray — Ray 2.42.1

Having a stable port for every launch might help with the issues you’re facing. But since you’re doing it in a testing environment I think doing it in Python will be better than the CLI. The general idea is the same as the one above. I think the equivalent in Python code will be ray.init(address="127.0.0.1:6379") , or something like this in your test suite:

  ray.init(
      address=127.0.0.1:6379,      ...
  )

I think either one will work in setting the port, lmk if this helps in your tests.

Thanks, @christina, but this won’t work because if an address is specified, ray.init() will try to connect to it and not create an instance.

According to the docs, the only way to have ray.init() create a new local instance is not to specify an address.

And yes, I cannot use the cli because the init needs to happen in the test only.

FYI I resolved this for now by using the address="auto" argument in ray.init()

# project/utils.py
def is_ray_local():
    """Logic to determine whether you are running locally or in a deployment"""
    return True

# project/service.py
import ray

from project.utils import is_ray_local


RAY_LOCAL_FLAG = is_ray_local()
RAY_CLUSTER_ENDPOINT_ACTUAL = "ray://1.1.1.1:11111"

def get_ray_enpoint()
    if RAY_LOCAL_FLAG:
        return "auto"
    return RAY_CLUSTER_ENDPOINT_ACTUAL

def foo_service():
    ray.init(address=get_ray_enpoint())
    # Do stuff
    return "Success"

# tests/test.py
import ray

from project.service import foo_service


def test_foo_service():
    ray.init()

    status = foo_service()

    assert status == "Success"

Ok! Thanks for the update :slight_smile: I read the docs for a bit and here’s just a small update with what I found out so far. I think your workaround works well, too. ^^ You can also set the environment variable RAY_ADDRESS and then ray.init() will automatically use the environment variable that you set previously automatically. I think this should work even if you are running test suites. Apologies, I wasn’t much help, but please feel free to reach out anytime!