ComputeContext#

class polars_nanook.ComputeContext(
*,
name: str | None = None,
cpus: int | None = None,
memory: int | None = None,
cpu_architectures: list[CPUArchitecture] | None = None,
instance_type: str | None = None,
storage: int | None = None,
cluster_size: int | None = None,
requirements: str | Path | io.IOBase | bytes | None = None,
env_vars: dict[str, str] | None = None,
connection_mode: ConnectionMode | None = None,
workspace: str | UUID | Workspace | None = None,
labels: list[str] | str | None = None,
log_level: LogLevel | None = None,
idle_timeout_mins: int | None = None,
provider: ComputeProvider | None = None,
region: str | None = None,
insecure: bool = False,
_specs: pcr.ComputeContextSpecs | None = None,
)

Compute context in which queries are executed.

The compute context is an abstraction of the underlying hardware (either a single node or multiple nodes in case of a cluster).

Parameters:
name

The name of the registered ComputeContext to start, or connect to if already running.

cpus

The number of CPUs each instance in the compute context should have access to. This acts as a lower bound, Polars Nanook finds the smallest available machine that satisfies both cpu and memory requirements.

memory

The amount of RAM (in GB) each instance in the compute context should have access to. This acts as a lower bound, see cpus.

cpu_architectures

The cpu architectures to consider. Defaults to arm64, serverless compute only supports arm64.

instance_type

The instance type to use.

storage

The amount of disk space (in GB) each instance in the compute context should have access to. Not supported on serverless compute.

cluster_size

The number of machines to spin up in the cluster. Defaults to 1.

requirements

Path to a file or a file-like object [1] containing dependencies to install in the compute context, in the requirements.txt format.

env_vars

dict of environment variables to override while running Polars queries.

connection_mode

How the context will connect to the compute cluster. - direct: connect directly to the compute cluster. - proxy: send queries to the compute cluster via the control plane. Defaults to direct.

workspace

The workspace to run this context in. You may specify the name (str), the id (UUID) or the Workspace object. If you’re in multiple organizations that have a workspace with the same name then you need to explicitly specify the organization.

labels

Labels of the workspace (will be implicitly created)

log_level{‘info’, ‘debug’, ‘trace’}

Override the log level of the context for debug purposes.

idle_timeout_mins

How many minutes a cluster can be idle before it will be automatically killed. The minimum is 10 minutes, by default it is set to 1 hour.

provider{‘aws’, ‘serverless’}

Where Polars Nanook provisions the compute context. - aws: in the workspace’s connected AWS account. - serverless: on compute managed by Polars Nanook. Defaults to NANOOK_DEFAULT_PROVIDER, then the workspace’s default provider.

region

The region to run serverless compute in, e.g. "us-east-2". Pick the region your data is in. Defaults to the workspace’s default region. Not supported with provider="aws".

Notes

Note

If the cpus, memory, and instance_type parameters are not set, the parameters are resolved with the workspace’s AWS cluster defaults; serverless compute has no defaults, so cpus and memory must be set.

Footnotes

Examples

>>> ctx = pn.ComputeContext(
...     workspace="workspace-name",
...     cpus=24,
...     memory=24,
...     cluster_size=2,
...     labels="docs",
... )
>>> ctx
ComputeContext(
    id=None,
    cpus=24,
    memory=24,
    instance_type=None,
    storage=None,
    cluster_size=2,
    mode="direct",
    workspace_name="workspace-name",
    labels=["docs"],
    log_level=LogLevelModel.Info,
)
>>> ctx.register("compute-name")
>>> pn.ComputeContext(workspace="workspace-name", name="compute-name")
ComputeContext(
    name="cluster-name",
    workspace_name="workspace-name",
    id=None,
    cpus=24,
    memory=24,
    instance_type=None,
    storage=None,
    cluster_size=2,
    mode="direct",
    labels=["docs"],
    log_level=LogLevelModel.Info,
)

The ComputeContext can also be used as a decorator or context manager, which will set it as the default context within its scope, automatically starting and stopping the underlying compute:

@pn.ComputeContext(workspace="workspace-name", cpus=96, memory=256)
def run_queries():
    ...
    query1.remote().execute()
    query2.remote().execute()

# or

with pn.ComputeContext(workspace="workspace-name", cpus=96, memory=256):
    ...
    query1.remote().execute()
    query2.remote().execute()

Methods:

get_status

Get the status of the compute context.

register

Register the compute cluster specs under the given name.

start

Start the compute context.

stop

Stop the compute context.

list

List all compute contexts in the workspace and the current status for each.

connect

Reconnect with an already running compute context by id.

select

Connect to existing compute context interactively.

cluster_info

Get static information about the cluster.

show_versions

Print the versions of the compute plane components.

Attributes:

cpus

The number of CPUs each instance has access to.

memory

The amount of RAM (in GB) each instance has access to.

cpu_architectures

The cpu_architectures of the compute context.

instance_type

The instance type of the compute context.

storage

The amount of disk space (in GB) each instance has access to.

cluster_size

The number of compute nodes in the context.

region

The region serverless compute runs in.

connection_mode

In what way to connect to the compute cluster.

labels

The labels of the compute context.

organization

The organization to run the compute context in.

workspace

The workspace to run the compute context in.

get_status() → ComputeContextStatus

Get the status of the compute context.

Examples

>>> ctx = pn.ComputeContext(workspace="workspace-name", cpus=24, memory=24)
>>> ctx.get_status()
UNINITIALIZED
register(name: str) → None

Register the compute cluster specs under the given name.

This does not start the compute cluster, instead allows the cluster to be started with this name in the future.

Parameters:
name

Name to register the compute cluster under.

Examples

>>> ctx = pn.ComputeContext(workspace="workspace-name", cpus=24, memory=24)
>>> ctx.register("my-cluster")
>>> ctx.start()
>>> ctx = pn.ComputeContext(workspace="workspace-name", name="my-cluster")
>>> ctx.start()
start(*, wait: bool = False) → None

Start the compute context.

This boots up the underlying node(s) of the compute context.

Parameters:
wait

Wait for the compute context to be ready before returning. If the ComputeContext is in direct connection mode, it will always wait until ready.

Examples

>>> ctx = pn.ComputeContext(workspace="workspace-name", cpus=24, memory=24)
>>> ctx.start()
stop(*, wait: ~typing.Any = <object object>) → None

Stop the compute context.

Parameters:
wait

Deprecated. This parameter no longer affects behavior.

Examples

>>> ctx = pn.ComputeContext(workspace="workspace-name", cpus=24, memory=24)
>>> ctx.stop()
classmethod list(
workspace: Workspace | UUID | str,
) → list[tuple[Self, ComputeContextStatus]]

List all compute contexts in the workspace and the current status for each.

Parameters:
workspace

Name or ID of the workspace the compute context lives in

Examples

>>> pn.ComputeContext.list(workspace="YourWorkspace")
[(ComputeContext(...), ComputeContextStatus)],
[(ComputeContext(...), ComputeContextStatus)],
[(ComputeContext(...), ComputeContextStatus)],
classmethod connect(
compute_id: str | UUID,
workspace: str | UUID | Workspace | None = None,
) → Self

Reconnect with an already running compute context by id.

Parameters:
workspace

The workspace in which the compute context lives

compute_id

The unique identifier of the existing compute context.

Examples

>>> ctx = pn.ComputeContext.connect(
...     workspace="WorkspaceName",
...     compute_id="xxxxxxxx-1860-7521-829d-40444726cbca",
... )
classmethod select(workspace: str | UUID | Workspace | None = None) → Self | None

Connect to existing compute context interactively.

Parameters:
workspace

The workspace in which the compute context lives

Examples

>>> ctx = pn.ComputeContext.select(
...     workspace="WorkspaceName",
... )
property cpus: int | None

The number of CPUs each instance has access to.

property memory: int | None

The amount of RAM (in GB) each instance has access to.

cluster_info() → ClusterInfo

Get static information about the cluster.

The information is fetched from the cluster’s scheduler once and cached until the connection to the cluster is reset, e.g. by stopping the compute context.

Raises:
RuntimeError

If the compute context is not accessible or not in direct connection mode.

Examples

>>> ctx = pc.ClusterContext(uri="http://localhost")
>>> ctx.cluster_info().anonymous_result_location
's3://bucket/query_outputs/'
property cpu_architectures: list[CPUArchitecture] | None

The cpu_architectures of the compute context.

show_versions() → None

Print the versions of the compute plane components.

Raises:
RuntimeError

If the compute context is not accessible or not in direct connection mode.

Examples

>>> ctx = pn.ClusterContext(uri="http://localhost")
>>> ctx.show_versions()
Compute Plane Version: 0.1.0
Polars Python Version: 1.38.1
Polars Rust Revision: c1764bcd298d3d2cfab4629eb0a5c36be412786e
property instance_type: str | None

The instance type of the compute context.

property storage: int | None

The amount of disk space (in GB) each instance has access to.

property cluster_size: int

The number of compute nodes in the context.

property region: str | None

The region serverless compute runs in.

property connection_mode: str

In what way to connect to the compute cluster.

property labels: list[str] | None

The labels of the compute context.

property organization: Organization

The organization to run the compute context in.

property workspace: Workspace

The workspace to run the compute context in.