To connect to Anyscale, initiate an interactive session with
ray.init. This API call takes a Ray address, as specified either in an environment variable or as an argument. The code described here in this section comes into play when the Ray address begins with the string "
Ray address as an environment variable
The URL can be provided by setting the
RAY_ADDRESS environment variable, e.g.:
RAY_ADDRESS=anyscale:// python script.py
Ray address embedded in code
The URL can also be provided explicitly in code, e.g.:
Specifying cluster properties
The shortest possible Ray address for Anyscale is
anyscale://. You can optionally set a name for the project and cluster, e.g.,
my-cluster, by adding it to the string, e.g.,
anyscale://my-project/my-cluster. A cluster name can be specified without a project name as
anyscale://my-cluster, in which case the cluster will be created in the project specified by the
ANYSCALE_PROJECT_NAME environment variable or in the default project. If a project name is specified without a cluster name, a cluster with an autogenerated name will be created in that project. A single name will be considered a project name if it is followed by a trailing slash, e.g.
Additionally, you add the following optional properties as query parameters:
update: Whether to update cluster configurations when connecting to an existing cluster. Note that setting this parameter generally triggers a cluster restart. By default,
updateis set to False, and Anyscale will not restart a cluster to pick up configuration changes.
cluster_env: The name (and optionally revision) of the. cluster environment or a dictionary to build a new cluster environment. If no revision is specified, use the latest revision.
cluster_compute: The name of the cluster compute to use.
autosuspend: An integer representing the number of minutes before the cluster autosuspends if idle. By default,
autosuspendis set to 120 minutes. It can be disabled by setting the value to -1.
A fully-formed example of Ray address looks as follows:
When setting the Ray address in code, you can configure your cluster using keyword arguments in your call to
ray.init("anyscale://my-cluster", job_name="production_job", cloud="aws_test_cloud")
The following keyword arguments are supported:
|Keyword Argument||Description||Example values|
|Whether to update cluster configurations when connecting to an existing cluster. Note that this may restart the Ray runtime. By default, update is set to False.|
|The name of the cluster compute to use or a dictionary to build a new cluster compute.|
|The name (and optionally revision) of the cluster environment or a dictionary to build a new cluster environment. If no revision is specified, use the latest revision.|
|A python dictionary with runtime environment specifications.|
|The name of the cloud to start the cluster in.|
|Deprecated: Use |
|Deprecated: Specify project name in connection string or in |
|The initial number of CPUs to scale to. The cluster will immediately scale to accommodate the requested number of CPUS, bypassing normal upscaling speed constraints. The requested CPUs are pinned and exempt from downscaling.|
|The initial number of GPUs to scale to. The cluster will immediately scale to accommodate the requested number of GPUS, bypassing normal upscaling speed constraints. The requested GPUs are pinned and exempt from downscaling.|
|A list of initial resource bundles to scale to. Each bundle is a dictionary mapping a resource name to a quantity that can be allocated on a single machine. The cluster will immediately scale to accommodate the requested resources, bypassing normal upscaling speed constraints. The requested resources are pinned and exempt from downscaling.|
|Either an integer representing the number of minutes before the cluster autosuspends (after being idle), or a string with |
|The name of the job, which will be shown in the UI. This name is only used for display purposes.|
|The name of the namespace to run this cluster in.|
ray.init("anyscale://...") returns a
ClientContext object. This object provides information about the cluster (Python version, Ray version, and a URL to the Ray dashboard). The
ClientContext also provides a
disconnect() method that will disconnect from the current cluster. Finally, you may also use
ClientContext as a context manager, which will automatically call
disconnect() for you after the
with block is complete.
context = ray.init("anyscale://cluster1")
... # Computation in Cluster 1
context.disconnect() # Manually disconnect
# Start client using a `with` statement
with ray.init("anyscale://cluster2") as c2:
... # do things with c2
# Automatically disconnect from cluster 2 after exiting the `with` block
For more information on
ClientContext see the Ray Documentation for Ray Client