Multiple ray.init()

This may be a dumb question.
I often run multiple scripts that each execute an experiment/training withtune.run. And each script requires a ray.init. However, there may already be an existing Ray cluster in my company’s server and I’d really like to not use up unnecessary resources by starting another set of raylet, workers, etc.

Thus, I made a function that checks for any existing ray cluster, and if there is none, it starts a new one; if there is an existing cluster, it has the address=auto setting. See below:

import ray
import psutil


def ray_init():
    process_status = [proc for proc in psutil.process_iter() if proc.name() == "raylet"]

    if process_status:
        ray.init(include_dashboard=False, ignore_reinit_error=True, local_mode=False, address="auto")
    else:
        ray.init(include_dashboard=False, ignore_reinit_error=True, local_mode=False)

Is there a simpler/better method of doing this?

I think you can do this now with recent versions of Ray.

>>> import ray; ray.init(address="local")
2024-07-10 12:14:33,721	INFO worker.py:1740 -- Started a local Ray instance. View the dashboard at 127.0.0.1:8265
RayContext(dashboard_url='127.0.0.1:8265', python_version='3.9.19', ray_version='2.23.0', ray_commit='a0947ead5cd94b3d8ca5cdeb9422dccb12d03867')

>>> ray.init(address="local", ignore_reinit_error=True)
2024-07-10 12:14:50,718	INFO worker.py:1582 -- Calling ray.init() again after it has already been called.
RayContext(dashboard_url='127.0.0.1:8265', python_version='3.9.19', ray_version='2.23.0', ray_commit='a0947ead5cd94b3d8ca5cdeb9422dccb12d03867')

The second ray.init() won’t do anything since there’s already an instance running locally.