Job
- class lightning_sdk.Job(name, teamspace=None, org=None, user=None, *, _fetch_job=True, _num_machines=1)
Bases:
objectSubmit and manage jobs on the Lightning AI Platform.
Fetch already existing jobs.
- Parameters:
name (
str) – the name of the job.teamspace (
Union[str,Teamspace,None]) – the teamspace the job is part of.org (
Union[str,Organization,None]) – the name of the organization owning theteamspacein case it is owned by an org. Deprecated — pass the owner as part ofteamspaceinstead, e.g.teamspace="owner/teamspace".user (
Union[str,User,None]) – the name of the user owning theteamspacein case it is owned directly by a user instead of an org. Deprecated — pass the owner as part ofteamspaceinstead, e.g.teamspace="owner/teamspace".name (str)
org (str | Organization | None)
_fetch_job (bool)
_num_machines (int)
- Raises:
ValueError – If the teamspace cannot be resolved from the provided arguments, or if the job is not found when
_fetch_job=True.PermissionError – If the user does not have access to jobs in the given teamspace.
- classmethod run(name, machine, cloud=None, command=None, studio=None, image=None, teamspace=None, org=None, user=None, env=None, interruptible=False, image_credentials=None, cloud_account_auth=False, entrypoint=None, path_mappings=None, max_runtime=None, max_run_attempts=None, reuse_snapshot=True, scratch_disks=None, placement_group_id=None, num_machines=1, tags=None, keep_minutes=None)
Run async workloads using a docker image or a compute environment from your studio.
- Parameters:
name (
str) – The name of the job. Needs to be unique within the teamspace.machine (
Union[Machine,str]) – The machine type to run the job on.num_machines (
int) – The number of machines to run on. Defaults to one.command (
Optional[str]) – The command to run inside your job. Required if using a studio. Optional if using an image. If not provided for images, will run the container entrypoint and default command. Studio jobs run the command as a bash script withset -e, so the first command that fails ends the job with its exit code. That includes cleanup commands andEXITtraps that run afterexit 0.studio (
Union[Studio,str,None]) – The studio env to run the job with. Mutually exclusive with image. If bothstudioandimageare left unset and this code is running inside a Studio (detected via theLIGHTNING_CLOUD_SPACE_IDenv var), defaults to that Studio, provided its teamspace matches the resolvedteamspace.image (
Optional[str]) – The docker image to run the job with. Mutually exclusive with studio.teamspace (
Union[str,Teamspace,None]) – The teamspace the job should be associated with. Defaults to the current teamspace. Accepts a bare name or anowner/teamspaceslug.org (
Union[str,Organization,None]) – The organization owning the teamspace, if any. Defaults to the current organization. Deprecated — pass the owner as part ofteamspaceinstead, e.g.teamspace="owner/teamspace".user (
Union[str,User,None]) – The user owning the teamspace, if any. Defaults to the current user. Deprecated — pass the owner as part ofteamspaceinstead, e.g.teamspace="owner/teamspace".cloud (
Union[CloudProvider,str,None]) – Cloud provider or cloud account to run the job on.env (
Optional[Dict[str,str]]) – Environment variables to set inside the job.interruptible (
bool) – Whether the job should run on interruptible instances. Cheaper but can be preempted.image_credentials (
Optional[str]) – Credentials secret name used to pull a private image.cloud_account_auth (
bool) – Whether to authenticate with the cloud account to pull the image. Required if the registry is part of a cloud provider, such as ECR.entrypoint (
Optional[str]) – The entrypoint of your docker container. Defaults tosh -c. Set to an empty string to use the image’s pre-defined entrypoint with a command. Only applicable when submitting docker jobs.path_mappings (
Optional[Dict[str,str]]) – Maps container paths to data-connection paths in the form{"<CONTAINER_PATH>": "<CONNECTION_NAME>:<PATH>"}or{"<CONTAINER_PATH>": "<CONNECTION_NAME>"}for the root of a connection. Only applicable when submitting docker jobs.max_runtime (
Optional[int]) – How long, in seconds, to reserve a DWS (Dynamic Workload Scheduler) machine, such as some top-end GCP GPUs or Lightning baremetal. On GCP the job is stopped when the reservation ends. On Lightning baremetal the job keeps running afterwards and the machine becomes interruptible. For a hard time limit, wrap your command, e.g.timeout 7200 python train.py. Has no effect on interruptible (spot) machines.Nonemeans no reservation is requested.max_run_attempts (
Optional[int]) – Max number of run attempts for this job.Noneor0means unset (backend default).1means a single attempt (no retries).N > 1allows up toNattempts. For multi-machine jobs this is set at the multi-machine job level (not the per-machineJobSpec). When greater than1for a multi-machine job, theRECREATE_ALL_NODESfault tolerance strategy is set automatically on the multi-machine job body.reuse_snapshot (
bool) – Whether to reuse a Studio snapshot when multiple jobs for the same Studio are submitted. Turning this off may result in longer startup times. Defaults to True.scratch_disks (
Optional[Dict[str,int]]) – Optional mapping of scratch-disk mount paths to their sizes in GiB.placement_group_id (
Optional[str]) – Optional placement group identifier for colocating the job.tags (
Optional[List[str]]) – Teamspace tag names to apply to the job. Tags that don’t exist in the teamspace yet are created, which requires permission to create tags. Names are normalised by the platform (lowercased, whitespace collapsed), sojob.tagsmay read back slightly differently from what was passed in.keep_minutes (
Optional[int]) – Minutes to keep the machine after the job stops.Noneor0releases the machine immediately.name (str)
cloud (CloudProvider | str | None)
command (str | None)
image (str | None)
org (str | Organization | None)
interruptible (bool)
image_credentials (str | None)
cloud_account_auth (bool)
entrypoint (str | None)
max_runtime (int | None)
max_run_attempts (int | None)
reuse_snapshot (bool)
placement_group_id (str | None)
num_machines (int)
keep_minutes (int | None)
- Returns:
The newly submitted Job instance.
- Return type:
- Raises:
ValueError – If required arguments are missing or mutually exclusive arguments are both provided.
RuntimeError – If image and studio are both provided.
- download_artifacts(target_dir='.', path='')
Download what this job wrote to a local directory.
- Parameters:
- Raises:
RuntimeError – If the job keeps no artifacts. A container job only keeps them when it was launched with
artifacts_destination.- Return type:
- download_logs(*, timestamps=False)
Download the job’s stored logs to a file and return the written path.
Reads every page of the job’s history from the logs API. A multi-machine job downloads the merged timeline of all its machines, each line prefixed with its machine name. A finished job whose lines are not in the logs API yet raises
RuntimeError, aslogsdoes.
- list_artifacts(path='', recursive=False)
List what this job wrote to the teamspace drive.
- Parameters:
- Returns:
One entry per file or folder, empty when the job keeps no artifacts. For a multi-machine job every machine is listed, and each path is prefixed with the machine’s name.
- Return type:
List[FileEntry]
- rename(new_name)
Rename this job.
Only standalone jobs (not launched from a deployment, MMT, or pipeline) can be renamed. An empty name or a name that matches the current name raises a
ValueError.- Parameters:
- Raises:
ValueError – If
new_nameis empty or matches the current name.- Return type:
- set_tags(tags)
Replace this job’s tag set with the given tag names.
Tags that don’t exist in the teamspace yet are created, which requires permission to create tags. Pass an empty list to remove all tags.
- property artifacts_uri: str | None
The
lit://address of this job’s artifacts, forlightning lsandlightning cp.Nonewhen the job keeps no artifacts, which is the case for a container job launched withoutartifacts_destination, and for a multi-machine job, whose machines each have their own — iterate overjob.machines.
- property current_run_attempt: int | None
Current run attempt for this job, or
Noneif unset.For multi-machine jobs this reads the multi-machine job body; for standalone jobs it reads the per-job
spec.
- property is_multi_machine: bool
Whether this object represents a multi-machine parent job.
- property keep_minutes: int | None
Minutes the machine is kept after the job stops, or
Noneif unset.
- property logs: _Logs
The job’s logs.
Use it as a value for a snapshot of the logs up to now:
print(job.logs)or call it to pass options and/or follow the logs live:
recent = job.logs(tail=100) # snapshot of the last 100 lines for line in job.logs(follow=True): # stream new lines as they arrive print(line)Options:
follow: Keep the stream open and yield new lines as they are produced. Returns an iterator of lines instead of a string.tail: Only include the last N lines.rank: Machine (0-based) to read in a multi-machine job. The logs API has no rank parameter, so the machine is resolved client-side to its own job and read from the same logs API; equivalent tojob.machines[rank].logs.timestamps: Prepend each line with its ISO-8601 timestamp.since/until: Only include lines within this RFC3339 time range.query: Only include lines containing every whitespace-separated term.severity: Only include lines at or above this level (error,warning,infoordebug).
since,until,queryandseverityare applied by the server, to both the saved logs and the live stream.A finished job whose lines are not in the logs API yet is still being migrated from older storage, and raises
RuntimeError; reading it again shortly after succeeds. A running one with no history in the logs API has its live stream tailed briefly instead.
- property max_run_attempts: int | None
Max number of run attempts for this job, or
Noneif unset.For multi-machine jobs this reads the multi-machine job body; for standalone jobs it reads the per-job
spec.
- property num_machines: int
The number of machines allocated to this job.
- property started_at: datetime | None
When the job started running, or
Noneif it hasn’t started yet.
- property stopped_at: datetime | None
When the job stopped running, or
Noneif it hasn’t stopped yet.
Was this page helpful?
Thanks for the feedback.