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