2016-12-29 8 views
1

私はFutureのリストを作成したいと思います。それぞれのリストは合格または失敗し、成功したFutureの結果を照合することができます。これどうやってするの?Scalaの複数のFutureを処理する

val futures2:List[Future[Int]] = List(Future{1}, Future{2},Future{throw new Exception("error")}) 

質問 1)私は、各成功未来からの戻り値の合計を収集し、(私は3を取得する必要があります)失敗したものを無視したい) 2を終了するごと将来のために待ちたいです。

+0

あなたは手を差し伸べなければならないいくつの先物を知っていますか? –

+0

いいえ、サイズは限られています。しかし、私はf1、f2のような個別の値ではなく、List [Future]を使用したいと考えています。 –

+1

次のようなものです:http://stackoverflow.com/questions/20874186/scala-listfuture-to-futurelist-disregarding-failed -futures?rq = 1 –

答えて

2

あなたが理解する必要があるものは、... FutureまたはFuturesから値を取得しようとしないでください。

土地はFuturisticです。

val futureList = List(
    Future(1), 
    Future(2), 
    Future(throw new Exception("error")) 
) 

// addd 1 to futures 
// map will propagate errors to transformed futures 
// only successful futures will result in +1, rest will stay with errors 
val tranformedFutureList = futureList 
    .map(future => future.map(i => i + 1)) 

// print values of futures 
// simimlar to map... for each will work only with successful futures 
val unitFutureList = futureList 
    .map(future => future.foreach(i => println(i))) 

// now lets give you sum of your "future" values 
val sumFuture = futureList 
    .foldLeft(Future(0))((facc, f) => f.onComplete({ 
    case Success(i) => facc.map(acc => acc + i) 
    case Failure(ex) => facc 
    }) 

、以来、OP(@Manuチャンダは)私はPromiseはScalaであるかについて、いくつかのビットを付加しています、Promiseから値を「取得」について尋ねました。

まず、Futureについて考えてみましょう。Scalaです。

Future[Int]が表示されている場合は、Intと表示されているan ongoing computationと考えてみてください。計算はsuccessfully completeとなり、結果はSuccess[Int]またはthrow an exceptionとなり、結果はFailure[Throwable]となります。そして、あなたはonCompleterecoverWithonFailureのような計算を話すようなような機能を見ます。

val intFuture = Future { 
    // all this inside Future {} is going to run in some other thread 
    val i = 5; 
    val j = i + 10; 
    val k = j/5; 
    k 
} 

今は... Promiseとは何ですか?

名前が示すように... Promise[Int]は、Intという価値のものです。それ以上のものはありません。

親が子供に特定のおもちゃを約束したときと同じです。この場合、親が必ずしもそのおもちゃを手に入れ始めたわけではないことに注意してください。

約束を完了するためには、まずそれを完了するための作業を開始しなければなりません。市場に出...買い物をして...帰宅してください。忙しいので...他の人がそのおもちゃを親に持参しようとする(彼はそれを買うことができないかもしれない)、そして彼らは何でも彼らは彼から得た結果。

だから... Promiseは、Futureを内部にラップします。そして、"wrapped" Future "値"はPromiseの値と考えることができます。そう

...

println("Well... The program wants an 'Int' toy") 

// we "promised" our program that we will give it that int "toy" 
val intPromise = Promise[Int]() 

// now we can just move on with or life 
println("Well... We just promised an 'Int' toy") 

// while the program can make plans with how will it play with that "future toy" 

val intFuture = intPromise.future 
val plusOneIntFuture = intFuture.map(i => i + 1) 

plusOneIntFuture.onComplete({ 
    case Success(i) => println("Wow... I got the toy and modified it to - " + i) 
    case Failure(ex) => println("I did not get they toy") 
}) 

// but since we at least want to try to complete our promise 
println("Now... I suppose we need to get that 'Int' toy") 
println("But... I am busy... I can not stop everything else for that toy") 
println("ok... lets ask another thread to get that") 

val getThatIntFuture = Future { 
    println("Well... I am thread 2... trying to get the int") 
    val i = 1 
    println("Well... I am thread 2... lets just return this i = 1 thingy") 
    i 
} 

// now lets complete our promise with whatever we will get from this other thread 
getThatIntFuture.onComplete(intTry => intPromise.complete(intTry)) 

上記のコードは次のような出力になります、

Well... The program wants an 'Int' toy 
Well... We just promised an 'Int' toy 
Now... I suppose we need to get that 'Int' toy 
But... I am busy... I can not stop everything else for that toy 
Well... I am thread 2... trying to get the int 
Well... I am thread 2... lets just return this i = 1 thingy 
Wow... I got the toy and modified it to - 2 

PromiseFutureから "取得" 値のお手伝いをしていません。非同期プロセス(またはScalaのFuture)は別のファイルで実行されていますtimeline ...タイムラインをプロセスのタイムラインに合わせなければ、タイムラインで「価値」を「得る」ことはできません。

+0

私は非同期プロセスから何らかの価値を得るためにプロミスを使用しますか? –

関連する問題