Skip to content

Skaha Usage Examples

Creating a Session

Create a session
from skaha.session import Session

session = Session()
Spawn a session
session_id = session.create(
    name="test",
    image="images.canfar.net/chimefrb/testing:keep",
    cores=2,
    ram=8,
    kind="headless",
    cmd="env",
    env={"TEST": "test"},
    replicas=3,
)

This will create three headless containers, each with 2 cores and 8GB of RAM, and run the command env in each container. The environment variable TEST will be set to test in each container. The response will be a list of session IDs created.

print(session_id)
["mrjdtbn9", "ov6doae7", "g9b4p1p4"]

Container Replicas

When spawning sessions with the Skaha API, it adds two additional environment variables to each container:

  • REPLICA_COUNT: An integer representing the total number of replicas spawned, e.g. 3 for the example above.
  • REPLICA_ID: An integer representing the unique ID of the replica, e.g. 0, 1, 2 for the example above.

These environment variables can be used to configure your application to run in a distributed manner. For example, you can use the REPLICA_COUNT to configure the number of workers and the REPLICA_ID to configure the rank of the worker.

Private Container Registry Access

If you are using a private container image from the CANFAR Harbor Registry, you need to provide your harbor username and the CLI Secret through a ContainerRegistry object.

from skaha.models import ContainerRegistry
from skaha.session import Session

registry = ContainerRegistry(username="username", password="sUp3rS3cr3t")
session = Session(registry=registry)

Getting Session Information

Get session information
session.info(session_id)
Session Information
[{'id': 'g9b4p1p4',
  'userid': 'brars',
  'runAsUID': '166169204',
  'runAsGID': '166169204',
  'supplementalGroups': [34241, 34337, 35124, 36227, 1454823273, 1025424273],
  'appid': '<none>',
  'image': 'images.canfar.net/skaha/terminal:1.1.2',
  'type': 'headless',
  'status': 'Pending',
  'name': '2a74d03-1',
  'startTime': '2024-10-21T21:39:01Z',
  'expiryTime': '2024-11-04T21:39:01Z',
  'connectURL': 'not-applicable',
  'requestedRAM': '1G',
  'requestedCPUCores': '1',
  'requestedGPUCores': '0',
  'ramInUse': '<none>',
  'gpuRAMInUse': '<none>',
  'cpuCoresInUse': '<none>',
  'gpuUtilization': '<none>'}]

Getting Session Logs

To get the logs of a session, you can use the logs method. The response will be a dictionary with the session IDs as keys and the logs as values. The logs are plain text format and can be printed to the console.

Get session logs
session.logs(session_id)

Destroying a Session

When you are done with your session, you can destroy it using the destroy method. The response will be a dictionary with the session IDs as keys and a boolean value indicating whether the session was destroyed or not.

Destroy a session
session.destroy(session_id)
{"mrjdtbn9": True, "ov6doae7": True, "ayv4553m": True}