gpt4 book ai didi

java - akka - 如何获得多个 Actor 的结果?

转载 作者:行者123 更新时间:2023-12-02 12:46:03 25 4
gpt4 key购买 nike

我想知道是否有最佳实践可以向 2 个不同的参与者发送 2 条消息

并等待所有这些(当然得到结果)以便继续执行。

即:

send message to actor 1
send message to actor 2
List<results> = wait.all(actor1,actor2)

最佳答案

您可能正在寻找 ask patternFuture.sequencefor 结合使用:

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
}

关于java - akka - 如何获得多个 Actor 的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44775946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com