Wait for whichever task is first

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

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

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

Does the same work for Distributed.Future, too?

From the manual (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.

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