[英]akka - how to get results of several actors?
我想知道是否有最佳實踐將2條消息發送給2個不同的演員
並等待所有它們(當然會得到結果)才能繼續執行。
即類似:
send message to actor 1
send message to actor 2
List<results> = wait.all(actor1,actor2)
您可能正在尋找的詢問模式與組合Future.sequence
或for
-comprehension:
import akka.pattern.ask
case object Request
implicit val timeout = Timeout(5 seconds) // needed for `?` below
// Ask your actors for a result
val f1 = actorA ? Request
val f2 = actorB ? Request
val f3 = actorC ? Request
// for-comprehension
(for {
x <- f1
s <- f2
d <- f3
} yield (f1, f2, f3)).map {
case (r1, r2, r3) =>
//Do your stuff with the results
}
// Future.sequence
Future.sequence(f1, f2, f3).map {
case (r1, r2, r3) =>
//Do your stuff with the results
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.