首页 文章

如何解决Scala中的期货列表

提问于
浏览
22

我有一个返回Future的调用 . 但是,我需要拨打n个电话,这样我才会回来n期货 . 我想知道如何在继续之前获得所有解决方案(不阻止服务器)

例如,

while(counter < numCalls){
    val future = call(counter)
    future.map{  x =>
        //do stuff
    }
    counter += 1 
}

//Now I want to execute code here after ALL the futures are resolved without 
//blocking the server

3 回答

  • 5

    您可以使用 Future.sequence(futureList)List[Future[X]] 转换为 Future[List[X]] . 而且由于后者只是一个简单的 Future ,你可以在 Await.ready 或类似助手的帮助下等待它完成 .

    因此,您必须保留您生成的期货清单 . 就像是:

    val futures = new ListBuffer[Future[X]]
    while(counter < numCalls) {
        val future = call(counter)
        futures += future
        future.map { x =>
            //do stuff
        }
        counter += 1 
    }
    val f = Future.sequence(futures.toList)
    Await.ready(f, Duration.Inf)
    

    你也可以写成:

    val futures = (1 to numCalls).map(counter => {
        f = call(counter)
        f.map(x => ...)
        f
    })
    Await.ready(Future.sequence(futures), Duration.Inf)
    
  • 51

    更功能一点:

    val futures = for {
      c <- 0 until 10
       } yield {
        val f = call(c) 
        f onSuccess {
          case x =>
          // Do your future stuff with x
        }
        f
      }
    Future.sequence(futures)
    
  • 6

    我认为你想在期货结束后做点什么,比如说 . 一个回调,没有阻止原始呼叫?然后你应该做这样的事情:

    val futures = for (...) yield {
      future {
      ...
      }
    }
    
    val f = Future sequence futures.toList
    
    f onComplete {
      case Success(results) => for (result <- results) doSomething(result)
      case Failure(t) => println("An error has occured: " + t.getMessage)
    }
    

    http://docs.scala-lang.org/overviews/core/futures.html

    因此,您不会通过等待呼叫阻止,但您仍然等待所有期货完成,然后对所有结果执行某些操作 . 关键方面是使用Future.sequence将大量未来加在一起,然后使用回调来对结果进行操作 .

相关问题