# Running tasks in parallel (ray.get in a loop)

Why eugo.hpc or Ray tasks run one at a time when you call get inside the loop, and how to run them in parallel: submit every task first, call eugo.hpc.get once, use wait, batch tiny tasks and share large inputs with put.

Source: https://docs.eugo.io/docs/guides/parallel-tasks/

---

If your tasks run one at a time, you are probably calling `eugo.hpc.get` inside the loop that submits them. `.remote(...)` returns at once, but `eugo.hpc.get` waits for the result, so a `get` in the loop makes each task wait for the one before it. Submit every task first, then call `eugo.hpc.get` once on the whole list. Use `eugo.hpc.wait` to handle results as they finish, group tiny tasks into bigger ones, and share large inputs with `eugo.hpc.put`.

`eugo.hpc` is Ray's API under Eugo's name, so the same fix applies to `ray.get` in a loop.

> **Applies to:** the EugoLab image, November 2026.

## Before you start

- A notebook open in EugoLab.
- A running cluster in the **Eugo HPC Clusters** panel.
- The notebook connected to that cluster: on the cluster card, in the **Notebook** tab, select **Connect this notebook**. EugoLab adds and runs a two-line cell that connects the notebook. Connect before you run any task; otherwise the work runs on a private local Ray inside the notebook, not on the cluster. [Connecting a notebook to your cluster](/docs/guides/connecting-a-notebook.md) explains what Connect does and how to check the connection.

## Why do my tasks run one at a time?

`.remote(...)` returns at once with an `ObjectRef`, a reference to a result that may not exist yet. `eugo.hpc.get` blocks until that result is ready. With a list of references, it waits for all of them and returns the results in the same order as the list.

1. In a new cell, define the function once with `@eugo.hpc.distribute`:

   ```python
   import time
   import eugo.hpc

   @eugo.hpc.distribute
   def slow_square(x):
       time.sleep(0.5)
       return x * x
   ```

2. This loop is the slow pattern. Each `eugo.hpc.get` waits for its task before the next task is submitted, so the tasks run one after another:

   ```python
   results = []
   for i in range(100):
       results.append(eugo.hpc.get(slow_square.remote(i)))  # waits every time
   ```

3. This is the fast pattern. Submit every task, then call `eugo.hpc.get` once on the list. The tasks run at the same time, as far as the cluster's free CPUs allow:

   ```python
   refs = [slow_square.remote(i) for i in range(100)]  # returns at once
   results = eugo.hpc.get(refs)                          # waits once, results in order
   print(results[:5])
   ```

   ```text
   [0, 1, 4, 9, 16]
   ```

Define a remote function once and reuse it. Calling `eugo.hpc.distribute(fn)` inside a loop makes and exports a new remote function each time, which Ray lists as an anti-pattern.

## How do I handle results as they finish?

`eugo.hpc.wait(refs, num_returns=k, timeout=s)` returns two lists, `(ready, not_ready)`, as soon as `k` results are done or the timeout passes.

1. Submit the tasks, then take each result as soon as it is ready:

   ```python
   refs = [slow_square.remote(i) for i in range(10)]
   while refs:
       ready, refs = eugo.hpc.wait(refs, num_returns=1)
       print(eugo.hpc.get(ready[0]))
   ```

   The results print in the order the tasks finish, which need not be the order you submitted them.

## How do I speed up many tiny tasks?

Each task has a fixed scheduling cost. When the work in a task is smaller than that cost, group the work so each task does more.

1. Send a chunk of inputs to each task instead of one value:

   ```python
   @eugo.hpc.distribute
   def square_chunk(xs):
       return [x * x for x in xs]

   chunks = [list(range(i, i + 1000)) for i in range(0, 100_000, 1000)]
   results = eugo.hpc.get([square_chunk.remote(c) for c in chunks])
   print(len(results), len(results[0]))
   ```

   ```text
   100 1000
   ```

   The cell runs 100 tasks of 1,000 inputs each instead of 100,000 tasks of one input.

## How do I share a large input between tasks?

`eugo.hpc.put(value)` stores a value once in the cluster's object store and returns a reference. Pass the reference to every task instead of the value, so the value is not copied once per task.

1. Store the array once, then pass its reference:

   ```python
   import numpy as np

   big = np.random.rand(10_000, 1_000)
   big_ref = eugo.hpc.put(big)

   @eugo.hpc.distribute
   def column_mean(arr, col):
       return arr[:, col].mean()

   means = eugo.hpc.get([column_mean.remote(big_ref, c) for c in range(100)])
   print(len(means))
   ```

   ```text
   100
   ```

   A reference passed as a task argument arrives in the task as the value itself, so `column_mean` receives the array, not the reference. [Memory for distributed jobs](/docs/guides/memory-for-distributed-jobs.md) explains the object store and how big it is.

## Why do only some of my tasks run at once?

A function decorated with `@eugo.hpc.distribute(num_cpus=2)` reserves 2 CPUs for each call (the default is 1). How many tasks run at the same time is bounded by the cluster's free CPUs.

1. Check the cluster's free resources:

   ```python
   eugo.hpc.available_resources()
   ```

   The result is a dictionary. Its `CPU` entry is the number of CPUs free right now, and a cluster with GPU workers also has a `GPU` entry.

2. To run more at once, lower `num_cpus` for a light task, or give the cluster more or larger workers (see [Cluster settings](/docs/guides/cluster-settings.md)).

## Troubleshooting

### This notebook is already running Ray, started by a cell rather than through Connect

**Cause:** a cell ran work before you connected, so the notebook started its own Ray. The cluster card offers **Disconnect** instead of **Connect this notebook**, with this message as its tooltip.

**Fix:**

1. Stop the notebook's own Ray: on the cluster card, select **Disconnect**, or restart the notebook's kernel.
2. On the cluster card, in the **Notebook** tab, select **Connect this notebook**.
3. Run your task cells again.

### My tasks ran in the notebook, not on the cluster

**Cause:** without Connect, the first call that runs work (`.remote(...)`, `eugo.hpc.put` or `eugo.hpc.get`) starts a private local Ray inside the notebook.

**Fix:**

1. Stop the notebook's own Ray: on the cluster card, select **Disconnect**, or restart the notebook's kernel.
2. On the cluster card, in the **Notebook** tab, select **Connect this notebook**.
3. Run your task cells again.

### Error: eugo.hpc.use_auth_token_file: cannot use the Ray token file

The full message names the file and ends in "it does not exist", "it is a folder, not a file" or "this process is not allowed to read it".

**Cause:** the connect code was copied before an update, or before the lab restarted.

**Fix:**

1. On the cluster card, in the **Notebook** tab, select **Copy code**.
2. Paste the code into the notebook and run it.

If the notebook is already connected, the call raises `RuntimeError: eugo.hpc.use_auth_token_file: this process is connected to Ray; call eugo.hpc.shutdown() first`. Run `eugo.hpc.shutdown()`, then run the new code.

## Related

- [The eugo.hpc API](/docs/guides/eugo-hpc-api.md)
- [Distributed Python on a GPU cluster](/docs/guides/distributed-python.md)
- [Batch inference with Ray Data on GPUs](/docs/guides/batch-inference-ray-data.md)
- Ray: [Anti-pattern: Calling ray.get in a loop harms parallelism](https://docs.ray.io/en/latest/ray-core/patterns/ray-get-loop.html)
- Ray: [Anti-pattern: Over-parallelizing with too fine-grained tasks harms speedup](https://docs.ray.io/en/latest/ray-core/patterns/too-fine-grained-tasks.html)
- Ray: [Anti-pattern: Passing the same large argument by value repeatedly harms performance](https://docs.ray.io/en/latest/ray-core/patterns/pass-large-arg-by-value.html)
- Example notebooks: [map over a list](https://github.com/eugo-inc/ganymede/blob/main/ray_patterns/core/03_map-over-list.ipynb), [wait for the first ready result](https://github.com/eugo-inc/ganymede/blob/main/ray_patterns/core/07_wait-first-ready.ipynb), [put a shared object](https://github.com/eugo-inc/ganymede/blob/main/ray_patterns/core/09_put-shared-object.ipynb)
