Akka-如何获得几个演员的成绩? [英] akka - how to get results of several actors?

查看:82
本文介绍了Akka-如何获得几个演员的成绩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm wondering if there any best practice to send 2 messages to 2 different actors

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

and wait for all of them (get results of course )in order to continue executing.

即:

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


推荐答案

可能正在寻找询问模式结合 Future.sequence 进行理解:

You are probably looking for the ask pattern in combination with a Future.sequence or a 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
}

这篇关于Akka-如何获得几个演员的成绩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆