为什么Future.sequence并行而不是连续执行我的未来?

ssc*_*zio 35 scala future

" 序列 " 一词意味着一个接一个的一系列动作.

object Test {

  def main(args: Array[String]) {

    def producer() = {
      val list = Seq(
          future { println("startFirst"); Thread.sleep(3000); println("stopFirst") }, 
          future { println("startSecond"); Thread.sleep(1000); println("stopSecond") }
      )
      Future.sequence(list)
    }

   Await.result(producer, Duration.Inf)
  }
}
Run Code Online (Sandbox Code Playgroud)

因此我希望打印这个程序: startFirst stopFirst startSecond stopSecond

甚至: startSecond stopSecond startFirst stopFirst

但不是(因为它发生): startFirst startSecond stopSecond stopFirst

为什么不调用此方法Future.parallel()?我应该用什么来保证期货的所有 Seq期货都是连续触发的(而不是并行的)?

gro*_*ter 38

期货同时运行,因为它们已经同时启动:).要按顺序运行它们,您需要使用flatMap:

Future { println("startFirst"); 
         Thread.sleep(3000); 
         println("stopFirst") 
        }.flatMap{
         _ =>  Future { 
                       println("startSecond"); 
                       Thread.sleep(1000); 
                       println("stopSecond") 
               }
        }
Run Code Online (Sandbox Code Playgroud)

Future.sequence刚刚转变Seq[Future[T]] => Future[Seq[T]],这意味着收集所有已经开始的未来的结果并将其放在未来.

  • 序列是M [Future [A]] => Future [M [A]] (9认同)

eag*_*uan 11

对原始Future.sequence的一点改动将使未来的执行序列化:

def seq[A, M[X] <: TraversableOnce[X]](in: M[() => Future[A]])(implicit cbf: CanBuildFrom[M[()=>Future[A]], A, M[A]], executor: ExecutionContext): Future[M[A]] = {
    in.foldLeft(Future.successful(cbf(in))) {
       (fr, ffa) => for (r <- fr; a <- ffa()) yield (r += a)
    } map (_.result())
}
Run Code Online (Sandbox Code Playgroud)

你的代码看起来像这样:

object Test {
def main(args: Array[String]) {

    def producer() = {
      val list = Seq(
          {() => future { println("startFirst"); Thread.sleep(3000); println("stopFirst") }}, 
          {() => future { println("startSecond"); Thread.sleep(1000); println("stopSecond") }}
      )
      FutureExt.seq(list)
    }

    Await.result(producer, Duration.Inf)
    }
}
Run Code Online (Sandbox Code Playgroud)

这与原始代码非常相似,并且与原始Future.sequence()具有相同的结果集合


Vik*_*ang 5

线性化:

import scala.concurrent._
import scala.collection.mutable.Builder
import scala.collection.generic.CanBuildFrom
import language.higherKinds

/**
 * Linearize asynchronously applies a given function in-order to a sequence of values, producing a Future with the result of the function applications.
 * Execution of subsequent entries will be aborted if an exception is thrown in the application of the function.
 */
def linearize[T, U, C[T] <: Traversable[T]](s: C[T])(f: T => U)(implicit cbf: CanBuildFrom[C[T], U, C[U]], e: ExecutionContext): Future[C[U]] = {
  def next(i: Iterator[T], b: Builder[U, C[U]]): Future[C[U]] = if(!i.hasNext) Future successful b.result else Future { b += f(i.next()) } flatMap { b => next(i, b) }
  next(s.toIterator, cbf(s))
}

scala> linearize(1 to 100)(_.toString) foreach println

scala> Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)
Run Code Online (Sandbox Code Playgroud)

来自:https : //gist.github.com/viktorklang/3347939