# Handling Exceptions from list of tasks using ray.get

**URL:** <https://discuss.ray.io/t/handling-exceptions-from-list-of-tasks-using-ray-get/2538>\
**Category:** Ray Core\
**Created:** [June 16, 2021, 10:32pm UTC](https://discuss.ray.io/t/handling-exceptions-from-list-of-tasks-using-ray-get/2538 "2021-06-16T22:32:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javier\_Bosch](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/javier_bosch/32/150_2.png) [@Javier\_Bosch](https://discuss.ray.io/u/Javier_Bosch)\
**Post date:** [June 16, 2021, 10:32pm UTC](https://discuss.ray.io/t/handling-exceptions-from-list-of-tasks-using-ray-get/2538/1 "2021-06-16T22:32:53Z")

</div>

What is the correct way of handling an exception from a a list of tasks executed with ray.get?

```auto
ray.init()

@ray.remote(max_retries=5)
def f(i):
    try:
        save(i)
    except Exception:
        raise Exception

 x = ray.get[f.remote(i) for i in range(20)]

```

whereby it is possible that one of the tasks in f could raise an exception, but you would like the other tasks to complete.

A use case for this would be saving data to disk. If a save from one of the tasks fails for a dataset in one of the task workers, then a solution would be to retry the function.

What would be the best way to handle this problem. Also I am not referring to a workercrasherror, but I guess it can be thought of as inevitably crashing when an Exception is raised.

I have tried:

```auto
try:
    x = ray.get([f.remote(i) for i in range(20)])
except RayError as e: 
    print(e.pid) 

```

However, I only every get back the pid of 1 of the tasks that fails and not multiple pid(s) if more than one task raises and Exception.

---

<div class="post-metadata">

**Author:** ![sangcho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sangcho/32/425_2.png) [@sangcho](https://discuss.ray.io/u/sangcho)\
**Post date:** [June 16, 2021, 11:33pm UTC](https://discuss.ray.io/t/handling-exceptions-from-list-of-tasks-using-ray-get/2538/2 "2021-06-16T23:33:43Z")

</div>

Have you tried using ray.wait? E.g.,

```auto
# There will be at max `num_returns` length of ready per each function call
ready, unready = ray.wait([f.remote(i) for i in range(20)], num_returns=1) 
while unready:
    try:
        ray.get(ready)
    except Execption as e:
        # do whatever you want
    ready, unready = ray.wait(unready, num_returns=1) 

```

---

<div class="post-metadata">

**Author:** ![Javier\_Bosch](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/javier_bosch/32/150_2.png) [@Javier\_Bosch](https://discuss.ray.io/u/Javier_Bosch)\
**Post date:** [June 16, 2021, 11:50pm UTC](https://discuss.ray.io/t/handling-exceptions-from-list-of-tasks-using-ray-get/2538/3 "2021-06-16T23:50:09Z")

</div>

I am not sure this would solve the problem.

If the exception occurs, would it be assigned to `unready`. If so, this would trigger an infinite loop.

Otherwise, I am unsure how to gracefully catch an errors from 1 or more tasks in a call to `ray.get[list_of_tasks]`

---

<div class="post-metadata">

**Author:** ![sangcho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sangcho/32/425_2.png) [@sangcho](https://discuss.ray.io/u/sangcho)\
**Post date:** [June 17, 2021, 12:04am UTC](https://discuss.ray.io/t/handling-exceptions-from-list-of-tasks-using-ray-get/2538/4 "2021-06-17T00:04:18Z")

</div>

> If the exception occurs, would it be assigned to `unready` . If so, this would trigger an infinite loop.

No, if the object ref contains the exception, it is considered as ready (ready, but has the error message as a content \<= impl detail).

> Otherwise, I am unsure how to gracefully catch an errors from 1 or more tasks in a call to `ray.get[list_of_tasks]`

As I mentioned in the code block, setting `num_returns=1` will always ensure that the length of `ready` is always 1. So you can handle each object ref using this way.

---

<div class="post-metadata">

**Author:** ![Javier\_Bosch](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/javier_bosch/32/150_2.png) [@Javier\_Bosch](https://discuss.ray.io/u/Javier_Bosch)\
**Post date:** [June 17, 2021, 12:16am UTC](https://discuss.ray.io/t/handling-exceptions-from-list-of-tasks-using-ray-get/2538/5 "2021-06-17T00:16:53Z")

</div>

Wouldn’t this approach defeat the benefits of parallelism?

---

<div class="post-metadata">

**Author:** ![sangcho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sangcho/32/425_2.png) [@sangcho](https://discuss.ray.io/u/sangcho)\
**Post date:** [June 17, 2021, 12:29am UTC](https://discuss.ray.io/t/handling-exceptions-from-list-of-tasks-using-ray-get/2538/6 "2021-06-17T00:29:55Z")

</div>

ray.wait will return the future that is ready for the first time, and ray.get is guaranteed to return right away because ray.wait prepares the object locally already. It is a standard pattern in ray. Also note that num\_returns is 1 by default (so it is not different from using regular ray.wait). Which part do you think this defeats the benefit of parallelism?

---

<div class="post-metadata">

**Author:** ![Javier\_Bosch](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/javier_bosch/32/150_2.png) [@Javier\_Bosch](https://discuss.ray.io/u/Javier_Bosch)\
**Post date:** [June 17, 2021, 6:18am UTC](https://discuss.ray.io/t/handling-exceptions-from-list-of-tasks-using-ray-get/2538/7 "2021-06-17T06:18:09Z")

</div>

I was thinking `num_returns=1`

---

<div class="post-metadata">

**Author:** ![sangcho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sangcho/32/425_2.png) [@sangcho](https://discuss.ray.io/u/sangcho)\
**Post date:** [June 17, 2021, 6:32am UTC](https://discuss.ray.io/t/handling-exceptions-from-list-of-tasks-using-ray-get/2538/8 "2021-06-17T06:32:01Z")

</div>

num\_returns is 1 by default. So it should work in the same way as using regular ray.wait (note all tasks are running in parallel in the background already. You just bring 1 “ready” object locally using ray.wait)
