---
title: Batch jobs
description: >-
  The Jobs class in the Lightning SDK allows you to run batch jobs
  asynchronously inside a Studio. Use this to offload tasks like data
  processing, training...
canonical: 'https://lightning.ai/docs/platform/developers/sdk/batch'
---
# Batch jobs {#batch-jobs}

The Jobs class in the Lightning SDK allows you to run batch jobs asynchronously inside a Studio. Use this to offload tasks like data processing, training runs, or inference jobs that do not require interactive execution.

The SDK is automatically available inside Studios. To use the SDK outside a Studio, install it with:

```bash
pip install --upgrade lightning-sdk
```

Ensure you authenticate with `lightning login`.

```bash
lightning login
```

## Submit a Job {#submit-a-job}

Here is a full example of how to submit a job.

```python
# Install the Lightning SDK
# pip install lightning-sdk

from lightning_sdk import Studio, Machine, Job

# Step 1: Initialize a Studio
studio = Studio(name="batch-processing", teamspace="my-teamspace", user="my-user")
studio.start()

# Step 3: Run a Batch Job
job = Job.run(command="python process_data.py", name="data-job", machine=Machine.CPU, studio=studio)

# Step 4: Monitor Job Status
print(job.status)  # Running, Completed, Failed

# Step 5: Stop or Delete a Job
# job.stop()  # Gracefully stop a running job
# job.delete()  # Cancel and remove the job

# Step 6: Shut Down the Studio
studio.stop()

```

### Initialize a Studio {#initialize-a-studio}

Before running a job, first initialize and start a Studio:

```python
from lightning_sdk import Studio

studio = Studio(name="batch-processing", teamspace="my-teamspace", user="my-user")
studio.start()
```

### Submit the Job {#submit-the-job}

With the Studio defining the compute environment, submit the Job:

```python
job = Job.run(command="python process_data.py", name="data-job", machine=Machine.CPU, studio=studio)
```

### Submit a Job in a private cloud account {#submit-a-job-in-a-private-cloud-account}

Submitting a job on a private own cloud account is as simple specifying the `cloud_account` argument.

```python
job = Job.run(command="python process_data.py", name="data-job", machine=Machine.CPU, studio=studio, cloud_account="my-cloud-account-name")
```

### List running jobs {#list-running-jobs}

```python
teamspace = Teamspace("my-teamspace", user="my-user")

for job in teamspace.jobs:
    print(job.name, job.status)

```

### Check job status {#check-job-status}

```python
job = Job("data-job", teamspace="my-teamspace", user="my-user")
print(job.status)  # Running, Completed, Failed

```

### Stop or cancel a Job {#stop-or-cancel-a-job}

If a job is no longer needed, you can stop or delete it:

```python
job.stop()  # Gracefully stops a job
job.delete()  # Cancels and removes the job
```

## Run multiple jobs in parallel {#run-multiple-jobs-in-parallel}

You can launch multiple jobs on different machine types:

```python
Job.run(command="python preprocess.py", name="preprocess", machine=Machine.CPU, studio=studio)
Job.run(command="python train.py", name="train-job", machine=Machine.A10G, studio=studio)
Job.run(command="python infer.py", name="inference", machine=Machine.L4

```

## Automate Jobs {#automate-jobs}

### GitHub actions {#github-actions}

You can schedule jobs using GitHub Actions to run at a fixed time.

Unattended runs such as GitHub Actions can't sign in yet, because the service-account keys they need aren't available. The example below won't authenticate until they are.

```yaml
name: Scheduled Job

on:
  schedule:
    - cron: '0 3 * * *'  # Runs at 3 AM UTC daily

jobs:
  run_batch:
    runs-on: ubuntu-22.04
    steps:
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: "3.10"

      - name: Install Lightning SDK
        run: pip install lightning-sdk

      - name: Start a Job
        run: |
          python -c " \
              from lightning_sdk import Studio, Job, Machine; \
              studio = Studio(name='batch-pipeline', teamspace='my-team', user='ci-bot'); \
              studio.start(); \
              Job.run(command='python daily_task.py', name='daily-task', studio=studio, machine=Machine.A10); \
              studio.stop();"
```

- Use CPU machines for lightweight preprocessing tasks.
- Use GPU machines for deep learning inference and training.
- Enable interruptible mode for cost-saving on long-running jobs.
- Chain jobs using status checks to create multi-step pipelines.

## Job API {#job-api}

### __init__() {#init}

The __init__() method fetches an existing job and accepts the following arguments:

| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| name | str | The name of the Job | - |
| teamspace | str | Teamspace | The teamspace the job is part of | None, Inferred from environment |
| org | str | Organization | The name of the organization owning the teamspace in case it is owned by an org | None, Inferred from environment |
| user | str | User | The name of the user owning the teamspace in case it is owned by a user instead of an org | None, Inferred from environment |


**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", user="my-user")
```

### run() {#run}

The run() method submits a new job and accepts the following arguments:

| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| name | str | The name of the job | - |
| machine | Machine | Machine to run the Job on | - |
| command | str | The command to run inside the Studio | None, The command to run inside your job. Required is using a Studio as compute environment. Optional for docker jobs. In case it's not specified for docker jobs, these will run the default command specified by the image. |
| studio | Studio | str | None | The studio env to run the job with. Mutually exclusive with image. | None, Inferred from environment in case image is not specified. |
| image | str | None | The docker image to run the job with. Mutually exclusive with studio. | None |
| teamspace | Teamspace | str | None | The teamspace the job should be associated with. | None, Inferred from the studio if used as compute environment, else inferred from environment. |
| org | str | Organization | None | The organization owning the teamspace (if any). | None, inferred from either Studio (if used as compute environment), teamspace (if provided) or environment. |
| user | str | User | None | The user owning the teamspace (if any). | None, inferred from either Studio (if used as compute environment), teamspace (if provided) or environment. |
| cloud_account | str | None | The cloud account to run the job on. Previously this was called cluster. | None, inferred from the Studio (if used as compute environment) or environment if possible. Falls back to teamspace default cloud-account otherwise. |
| env | Dict[str, str] | None | Environment variables to set inside the job. | None |
| interruptible | bool | Whether the job should run on interruptible instances. They are cheaper but can be preempted. | False |
| image_credentials | str | None | The credentials used to pull the image. Required if the image is private. This should be the name of the respective credentials secret created on the Lightning AI platform. | None |
| 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 (e.g. ECR). | False |
| entrypoint | str | The entrypoint of your docker container. To use the pre-defined entrypoint of the provided image, set this to an empty string. Only applicable when submitting docker jobs. | "sh -c", which just runs the provided command in a standard shell. |
| path_mappings | Dict[str, str] | None | Dictionary of path mappings. The keys are the path inside the container whereas the value represents the data-connection name and the path inside that connection. Should be of form{    "<CONTAINER_PATH_1>": "<CONNECTION_NAME_1>:<PATH_WITHIN_CONNECTION_1>",    "<CONTAINER_PATH_2>": "<CONNECTION_NAME_2>"}If the path inside the connection is omitted it's assumed to be the root path of that connection.Only applicable when submitting docker jobs. | None |


**Example:**

```python
job = Job.run(command="python process_data.py", name="data-job", machine=Machine.T4, studio=studio)

```

### stop() {#stop}

Stops the respective job. Accepts no arguments.

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
job.stop()
```

### delete() {#delete}

Deletes the respective job. Note that this also deletes all respective artifacts and is non-reversible. Accepts no arguments.

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
job.delete()
```

### status {#status}

Property returning the current status of the job.

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
print(job.status) # -> Status.Pending, Status.Running, Status.Completed, Status.Stopped, Status.Failed
```

### machine {#machine}

Property returning the machine the job is/was running on.

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
print(job.machine) # -> Machine.A10G
```

### artifacts_uri {#artifacts-uri}

Property returning the `lit://` address of the job's artifacts in the teamspace drive. It is what [`lightning ls` and `lightning cp`](/docs/platform/developers/cli/drive.md) take, and what the Lightning AI web UI shows under the job.

`None` when the job keeps no artifacts. A job running a container image only keeps them when it was launched with `artifacts_destination`, and then they live at that destination rather than under `jobs/`.

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
print(job.artifacts_uri) # -> "lit://my-org/my-teamspace/jobs/data-job"
```

A Studio-backed job's files sit directly under `jobs/<job name>`, both in the drive and at `/teamspace/jobs/<job name>` in a Studio in the same teamspace, so a path copied out of a Studio addresses the drive as-is.

### list_artifacts() {#list-artifacts}

Lists what the job wrote to the teamspace drive. Returns one entry per file or folder, each with a `path`, `is_dir`, `size` and `last_modified`. Empty when the job keeps no artifacts.

| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| path | str | Subfolder of the job's artifacts to list. | "", all of them |
| recursive | bool | Whether to descend into subfolders. | False |

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
for entry in job.list_artifacts(recursive=True):
    if not entry.is_dir:
        print(f"{entry.path} ({entry.size} bytes)")
```

### download_artifacts() {#download-artifacts}

Downloads what the job wrote to a local directory. Raises `RuntimeError` when the job keeps no artifacts.

| Parameter | Type | Description | Default |
| --- | --- | --- | --- |
| target_dir | str \| Path | Local directory to download into. Created if it does not exist. | ".", the working directory |
| path | str | Subfolder of the job's artifacts to download. | "", all of them |

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
job.download_artifacts("./artifacts")

# Take one subfolder, so a long run does not have to come down whole
job.download_artifacts("./checkpoints", path="checkpoints")
```

### name {#name}

Property returning the name of the job.

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
print(job.name) # -> "data-job"
```

### teamspace {#teamspace}

Property returning the teamspace the job is in.

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
print(job.teamspace) # -> Teamspace("my-teamspace", org="my-org")
```

### studio {#studio}

Property returning the studio used as compute environment if it wasn't launched with a docker image.

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
print(job.studio) # -> Studio("my-studio", teamspace="my-teamspace", org="my-org") or None
```

### image {#image}

Property returning the image used as compute environment if it wasn't launched with a Studio.

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
print(job.image) # -> "ubuntu:latest" or None
```

### command {#command}

Property returning the command ran within the job.

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
print(job.command) # -> "python train.py"
```

### logs {#logs}

Property returning the logs generated by running the job. Will return an error if the job is still pending or running (check [job.status](/docs/platform/inference/batch-jobs/sdk.md#status))

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
print(job.logs) # -> "Epoch 1/2: ################"
```

### link {#link}

Property returning the link to view the job in the UI.

**Example:**

```python
job = Job("data-job", teamspace="my-teamspace", org="my-org")
print(job.link) # -> "https://lightning.ai/my-org/my-teamspace/studios/my-studio/app?app_id=jobs&job_name=data-job"
```

---

**Navigation**

- Parent: [SDK](/docs/platform/developers/sdk.md)
