Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for an unknown number of futures

Tags:

scala

future

In Scala 2.10, what is a correct way to write a function that returns a future which completes when all futures in a list complete?

After researching and experimenting, I have developed the code below, in a Scala Worksheet:

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent._

object ws2 {

  def executeFutures(futures: Seq[Future[Unit]]): Future[Unit] = {

    def cascadeFutures(futureSeq: Seq[Future[Unit]], f: Future[Unit]): Future[Unit] = {
      futureSeq match {
        case h :: t => h.flatMap { u => cascadeFutures(t, f) }
        case nil => f
      }
    }

    cascadeFutures(futures, Future {})
  }                                               //> executeFutures: (futures: Seq[scala.concurrent.Future[Unit]])scala.concurren
                                                  //| t.Future[Unit]


  Await.ready(executeFutures(Seq(
    Future { println("Future1") },
    Future { println("Future2") },
    Future { println("Future3") }
  )) , 2.seconds)                                 //> Future1
                                                  //| Future2
                                                  //| Future3
                                                  //| res0: awaitable.type = scala.concurrent.impl.Promise$DefaultPromise@2dd063b3
                                                  //| 

}

I'm not sure that this code is correct, or, even if it's correct, if there is a better way.

It's not a problem if the futures are executed serially instead of in parallel.

This question is different from Wait for several Futures, which deals with a known number of futures.

like image 603
Fernando Correia Avatar asked Apr 30 '14 18:04

Fernando Correia


1 Answers

Use Future.sequence to turn a List of Futures[T] into a single Future of List[T].

val listOfFutures:List[Future[T]] = ...
val futureOfList:Future[List[T]] = Future.sequence(listOfFutures)

This works, not just for Lists, but for any TraversableOnce, which includes most, if not all, of scala.collections.

like image 196
ggovan Avatar answered Oct 23 '22 09:10

ggovan