# Wait for whichever task is first

**URL:** <https://discourse.julialang.org/t/wait-for-whichever-task-is-first/64588>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 13, 2021, 3:59pm UTC](https://discourse.julialang.org/t/wait-for-whichever-task-is-first/64588 "2021-07-13T15:59:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [July 13, 2021, 3:59pm UTC](https://discourse.julialang.org/t/wait-for-whichever-task-is-first/64588/1 "2021-07-13T15:59:56Z")

</div>

Hi all,

does someone know a function which takes two Tasks (or Futures) and waits until one of them becomes available, cancels the other and returns the result?

best,  
Stephan

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 13, 2021, 4:19pm UTC](https://discourse.julialang.org/t/wait-for-whichever-task-is-first/64588/2 "2021-07-13T16:19:57Z")

</div>

You can use channels or conditions — e.g. define a `Condition` and have the tasks call `notify` on it when they are done. Then you can just `wait` on that condition.

See also this discussion: [How to call wait on multiple Tasks? - #4 by Jeff\_Emanuel](https://discourse.julialang.org/t/how-to-call-wait-on-multiple-tasks/50981/4)

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [July 13, 2021, 4:21pm UTC](https://discourse.julialang.org/t/wait-for-whichever-task-is-first/64588/3 "2021-07-13T16:21:17Z")

</div>

thanks a lot. That makes intuitive sense for Tasks. Awesome idea!

Does the same work for Distributed.Future, too?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 13, 2021, 4:40pm UTC](https://discourse.julialang.org/t/wait-for-whichever-task-is-first/64588/4 "2021-07-13T16:40:54Z")

</div>

> [@schlichtanders](#):
>
> Does the same work for Distributed.Future, too?

From [the manual](https://docs.julialang.org/en/v1/stdlib/Distributed/#Distributed.Future) (emphasis added):

> A `Future` is a placeholder for a **single** computation of unknown termination status and time. **For multiple potential computations, see `RemoteChannel` .**

In particular, “A `put!` on an already set `Future` throws an `Exception`”, so it’s not really suitable for use by multiple tasks.

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [July 13, 2021, 4:55pm UTC](https://discourse.julialang.org/t/wait-for-whichever-task-is-first/64588/5 "2021-07-13T16:55:52Z")

</div>

okay, so for Task `@async` I would use Condition or Channel, and for Distributed `@spawnnat` I would use RemoteChannel
