简体   繁体   English

在 Future.sequence 的成功上的片状

[英]Flaky onSuccess of Future.sequence

I wrote this method:我写了这个方法:

import scala.concurrent._
import ExecutionContext.Implicits.global
import scala.util.{ Success, Failure }

object FuturesSequence extends App {
  val f1 = future {
    1
  }

  val f2 = future {
    2
  }

  val lf = List(f1, f2)

  val seq = Future.sequence(lf)

  seq.onSuccess {
    case l => println(l)
  }
}

I was expecting Future.sequence to gather a List[Future] into a Future[List] and then wait for every futures (f1 and f2 in my case) to complete before calling onSuccess on the Future[List] seq in my case.我期待 Future.sequence 将 List[Future] 收集到 Future[List] 中,然后等待每个期货(在我的情况下为 f1 和 f2)完成,然后在我的情况下对 Future[List] seq 调用 onSuccess。

But after many runs of this code, it prints "List(1, 2)" only once in a while and I can't figure out why it does not work as expected.但是在多次运行此代码后,它只会偶尔打印一次“List(1, 2)”,我无法弄清楚为什么它没有按预期工作。

Try this for once,试试这个,

import scala.concurrent._
import java.util.concurrent.Executors
import scala.util.{ Success, Failure }

object FuturesSequence extends App {
  implicit val exec = ExecutionContext.fromExecutor(Executors.newCachedThreadPool)
  val f1 = future {
    1
  }

  val f2 = future {
    2
  }

  val lf = List(f1, f2)

  val seq = Future.sequence(lf)

  seq.onSuccess {
    case l => println(l)
  }
}

This will always print List(1,2) .这将始终打印List(1,2) The reason is simple, the exec above is an ExecutionContext of threads (not daemon threads) where as in your example the ExecutionContext was the default one implicitly taken from ExecutionContext.Implicits.global which contains daemon threads.原因很简单,上面的exec是一个线程(不是守护线程)的 ExecutionContext ,在您的示例中, ExecutionContext 是从ExecutionContext.Implicits.global隐式获取的默认值,其中包含守护线程。

Hence being daemon, the process doesn't wait for seq future to be completed and terminates.因此,作为守护进程,进程不会等待seq未来完成并终止。 if at all seq does get completed then it prints.如果seq确实完成了,那么它就会打印出来。 But that doesn't happen always但这并不总是发生

The application is exiting before the future is completes.应用程序在未来完成之前退出。

You need to block until the future has completed . 你需要阻塞直到未来完成 This can be achieved in a variety of ways, including changing the ExecutionContext, instantiating a new ThreadPool, Thread.sleep etc, or by using methods on scala.concurrent.Await这可以通过多种方式实现,包括更改 ExecutionContext、实例化新的 ThreadPool、Thread.sleep 等,或使用scala.concurrent.Await方法

The simplest way for your code is by using Await.ready .代码最简单的方法是使用Await.ready This blocks on a future for a specified amount of time.这会在指定的时间内阻止future In the modified code below, the application waits for 5 seconds before exiting.在下面修改后的代码中,应用程序在退出前等待 5 秒。

Note also, the extra import scala.concurrent.duration so we can specify the time to wait.还要注意,额外的 import scala.concurrent.duration以便我们可以指定等待的时间。

import scala.concurrent._
import scala.concurrent.duration._
import java.util.concurrent.Executors
import scala.util.{ Success, Failure }

object FuturesSequence extends App {
  val f1 = future {
    1
  }

  val f2 = future {
    2
  }

  val lf = List(f1, f2)

  val seq = Future.sequence(lf)

  seq.onSuccess {
    case l => println(l)
  }

  Await.ready(seq, 5 seconds)
}

By using Await.result instead, you can skip the onSuccess method too, as it will return the resulting list to you.通过使用Await.result ,您也可以跳过onSuccess方法,因为它会将结果列表返回给您。

Example:例子:

val seq: List[Int] = Await.result(Future.sequence(lf), 5 seconds)
println(seq)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM