Skip to main content

Interactive Sessions

Anyscale allows you to leverage remote resources on the cloud to distribute your workloads across multiple machines. During development, it is common to run the application's ray.init() entry point on your laptop while tasks and actors will run remotely on the Anyscale cluster. This makes it easy to iterate on your code.

Starting an interactive session

If you have an existing Ray application that runs on your laptop or on an open-source Ray cluster, it is easy to start an interactive session that runs it on Anyscale. All you need to do is make sure that the Ray address within ray.init() is pointing to Anyscale. This can be done either in your code or on the command line (both have the same effect).

Connection string in code

You can embed the connection string directly in your code:

script.py
# Run on local machine
ray.init()
script.py
# Run on open-source Ray cluster
ray.init("ray://<ip_address>:<port>")
script.py
# Run on Anyscale Ray cluster
ray.init("anyscale://my_cluster")

You can then run your script which will start the interactive session:

python script.py

You can specify additional parameters for the cluster creation, both as keyword arguments passed to ray.init, and as query parameters in the connection string.

Connection string as environment variable

Alternatively, you can avoid hardcoding a connection string in your Python script. The main advantage of this method is that you can switch your script between running locally (on your laptop), on an open-source Ray cluster, or an Anyscale Ray cluster without code changes.

Your script does not include any connection string:

script.py
# Run on either open-source or Anyscale Ray cluster
ray.init()

You can set the environment variable RAY_ADDRESS, and then run your script, e.g., script.py, which will start the interactive session:

# Run on local machine
python script.py
# Run on open-source Ray cluster
export RAY_ADDRESS=123.45.67.89:10001
python script.py
# Run on Anyscale Ray cluster
export RAY_ADDRESS=anyscale://my_cluster
python script.py

You can specify additional parameters for the cluster creation as query parameters in the connection string.

Auto-suspend

Anyscale clusters auto-terminate after 120 minutes of no activity unless otherwise specified during cluster creation. A cluster is considered to be active as long as you're running an interactive session from your laptop or directly on the cluster (e.g., using the cluster's Jupyter). When a session is active, the timeout will not start and the cluster will not be terminated.

To specify a different timeout for inactivity, you set a value for autosuspend in the keyword argument (in ray.init) or the query parameter (in the connection string). If you set this value to -1, the cluster will not be auto-terminated.

Running a Ray job as an interactive session

When you run the script on your local machine, if the driver on the local machine exits or disconnects, the job and all associated tasks and actors on the cluster will be killed. To avoid this problem, Ray supports Ray jobs, which can also run as interactive sessions on Anyscale. The script will run directly on the Anyscale cluster, enabling the local machine to disconnect without ending your job. This enables you to run jobs in the background (e.g., to run a job overnight).

To run Ray jobs as Anyscale interactive sessions, you can use the Ray CLI, the Ray Python SDK or the Ray CLI by setting the Ray address to the Anyscale cluster using the anyscale:// connection string. You can refer to the Ray job APIs documentation for more information on how to start Ray jobs.

Note Anyscale does not manage the lifecycle of clusters that Ray jobs run on. The command to submit the job might create a cluster on-demand, but that cluster will not be terminated when the job finishes unless there is an autosuspend value configured for the cluster. For this functionality and more features such as automatic retries, please see Production Jobs.

Running Ray Serve deployments in an interactive session

Ray Serve deployments run from interactive sessions will print out a url that can be used to query. The endpoints will be accessible to public internet traffic, but require an authentication token. Please see Restricting Internet Traffic for more information on how to query endpoints.