# Run jobs in the background

In this example we:

* Run a simple Python function on remote machines with `remote_parallel_map`.
* Set `detach=True` so the job keeps running if your laptop sleeps, loses internet, or you close your terminal.
* Check progress in the Burla dashboard while the job runs in the background.

## Before you start

Make sure you have already:

1. installed Burla: `pip install burla`
2. connected your machine: `burla login`
3. started your cluster in the Burla dashboard

## Step 1: Define a simple function

This function pretends to do long work by sleeping for 2 minutes, then prints a message.

```python
from time import sleep
from burla import remote_parallel_map

def process_number(number):
    sleep(120)
    print(f"Finished {number}")
```

## Step 2: Start the job in detached mode

Pass `detach=True` when you call `remote_parallel_map`:

```python
inputs = list(range(50))

remote_parallel_map(process_number, inputs, detach=True)
```

`inputs = list(range(50))` means Burla will run `process_number` 50 times (once for each number from 0 to 49).

After inputs finish uploading, Burla prints:

`Done uploading inputs! Job will now continue running if canceled locally.`

At this point, the job can continue on the cluster even if you stop the local process.

## Step 3: Let it run in the background

Once you see the upload-complete message, you can close your terminal or stop the script.\
Your functions keep running remotely.

Open the Burla dashboard and go to the **Jobs** tab to watch logs and progress.

## Step 4: Save results during a detached job

Because you are stopping the local script, save each result to the cluster filesystem (`./shared`) from inside your function.

```python
from pathlib import Path
from time import sleep
from burla import remote_parallel_map

def process_number(number):
    sleep(120)
    result_text = f"Finished {number}"
    output_path = Path(f"./shared/detach-example-results/result-{number}.txt")
    output_path.write_text(result_text)

inputs = list(range(50))
remote_parallel_map(process_number, inputs, detach=True)
```

After the job finishes, open the Burla dashboard and download files from `detach-example-results/` in the **Filesystem** tab.

## Why this is useful

Detached jobs are helpful when work may run for hours or days, such as:

* processing many files
* retraining many models
* running long simulations

You can submit the job once, let it run remotely, and check results later.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.burla.dev/all-examples/basic-examples/run-python-in-the-background.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
