Starting Ray Serve from Python

ray start --head# Start local Ray cluster.
serve start # Start Serve on the local Ray cluster.

Is it possible to start Ray and Ray Serve from a Python program by calling the above two commands using say, the subprocess command?

I was able to use the subprocess.run() command within a Python script to run ray start --head and then serve start locally. It launched a local Ray cluster and Serve instance, and I was able to add deployments to it.

Script to start Ray and Serve, and then launch deployments:

import subprocess

subprocess.run(["ray", "start", "--head"])
subprocess.run(["serve", "start"])

import ray
from ray import serve

ray.init(address="auto", namespace="serve")
serve.start()

@serve.deployment
def f():
    pass

# Deploy f to Serve instance started by subprocess
f.deploy()  # Stays deployed even after script exits

Script to check the deployment:

import ray
from ray import serve

ray.init(address="auto", namespace="serve")
print(serve.list_deployments())

Thank-you for the confirmation and the example code. Is the
serve.start()
line needed?

It doesn’t look like it is. I ran the first script without serve.start(), and it still worked.