- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力理解 Futures
并在 akka 中询问模式。
所以,我做了两个 Actor ,一个要求另一个给他发回信息。好吧,根据 akka 的 Futures
文档, Actor 应该向( ?
)询问消息,它会给他一个 Future
瞬间。然后actor应该阻塞(使用 Await
)来获取 Future
结果。
好吧,我永远不会完成我的 future 。为什么?
代码是:
package head_thrash
import akka.actor._
import akka.util.Timeout
import scala.concurrent.Await
import scala.concurrent.duration._
object Main extends App {
val system = ActorSystem("actors")
val actor1 = system.actorOf(Props[MyActor], "node_1")
val actor2 = system.actorOf(Props[MyActor], "node_2")
actor2 ! "ping_other"
system.awaitTermination()
Console.println("Bye!")
}
class MyActor extends Actor with ActorLogging {
import akka.pattern.ask
implicit val timeout = Timeout(100.days)
def receive = {
case "ping_other" => {
val selection = context.actorSelection("../node_1")
log.info("Sending ping to node_1")
val result = Await.result(selection ? "ping", Duration.Inf) // <-- Blocks here forever!
log.info("Got result " + result)
}
case "ping" => {
log.info("Sending back pong!")
sender ! "pong"
}
}
}
Duration.Inf
至
5.seconds
,然后 actor 等待 5 秒,告诉我的 future 已超时(通过抛出
TimeoutException
),然后其他 actor 最终回复所需的消息。所以,没有异步发生。为什么? :-(
最佳答案
Akka 官方documentation说 Await.result 将导致当前线程阻塞并等待 Actor 用它的回复“完成” future 。
奇怪的是,您的代码永远阻塞在那里,您的所有应用程序是否只有一个线程?
无论如何,我想一种更“惯用”的编码方式是对 future 的成功使用回调。
def receive = {
case "ping_other" => {
val selection = context.actorSelection("../node_1")
log.info("Sending ping to node_1")
val future: Future[String] = ask(selection, "ping").mapTo[String]
future.onSuccess {
case result : String ⇒ log.info("Got result " + result)
}
}
...
关于scala - Akka : the proper use of `ask` pattern?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20124072/
我一直在使用 Tensorflow 解决线性回归问题。我得到一条平坦的曲线 pred_y。我应该如何将我的曲线与观察的训练示例拟合? 这是我的 tensorflow 代码: # coding: utf
我浏览了这个网站,找不到类似的场景。我正在尝试运行以下代码 SELECT st.storeid, s.noofitems FROM salestrnsaction AS st, soldvia AS
请善待我,我是 Python 初学者:-) 现在,我发现编写 Python 程序的“最佳实践”是将主代码包装在“main”函数中,并执行 if "__main__"== __name__: 测试调用“
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 已关闭 9 年前。 Improve
这个问题在这里已经有了答案: What is the reason to use the 'new' keyword at Derived.prototype = new Base (6 个答案)
我正尝试在 tomcat 上为我的 jersey webapp 创建一个用户登录页面,它的行为与网络上的所有其他页面一样。也就是说,用户会看到一个漂亮的登录页面而不是弹出窗口(如 BASIC tomc
我正在尝试让 Android 服务监听来电,当确实有来电时,获取来电号码并向其发送短信。 在我的服务中,我制作了一个PhoneStateListener: TelephonyManager tMana
是否有适当的地方来存储数据库的高级描述?类似于“此数据库用于存储 XYZ 以供 ABC 使用”之类的内容。这不一定是人们需要查询的信息,而是对管理系统的人有用的信息(即几个月前我试图记住几个月前我试图
下面的排序逻辑非常简单: 1. 标记不包含所选排序值的产品 2. 按所选排序值的数量降序对产品进行排序 3.隐藏不包含所选排序值的产品 我添加了简单的排序逻辑,它可以对元素进行排序(可以在控制台中预期
这个问题在这里已经有了答案: Where does Visual Studio look for C++ header files? (7 个答案) 关闭 9 年前。 所以,我在 VS2010 中开
我正在尝试连接到 MSMQ 并发送一些消息。不幸的是,项目中有关于如何做到这一点的“先前艺术”,我应该模仿它。之前的应用程序的连接方式如下: if (MSMQ in workgroup mode) {
我正在编写一个可能会被其他人使用的 ROS 节点,这就是我想要遵循通用指南的原因。有时我想为异常添加额外的信息,但我不知道该怎么做。这是一个方便的示例: 我的 ROS 节点使用 boost 库连接到串
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 4 年前。 Improve
我在摆弄 django-tinyMCE并注意到一些配置没有得到应用。这是我的 settings.py 中的代码 TINYMCE_DEFAULT_CONFIG = { 'theme' : 'ad
回顾我过去的项目,我经常遇到这个: 一位客户或经理向我提出一项任务并要求进行估算。我估计说 24 小时。他们还询问了业务分析师,据我所知,他们的经验大多是非技术性的。他们给出的估计是 16 小时。最后
我使用以下方式调用几个sql文件: :r C:\Scripts\Script1.sql :r C:\Scripts\Script2.sql :r C:\Scripts\Script3.sql 我对 :
在类构造函数中初始化 AS3 类变量“更好”吗?或者当我在类的顶部声明它们时,我可以将它们初始化为默认值吗?我问这个问题是因为当有很多类变量时,在一个地方声明它们然后在另一个地方初始化它们似乎效率很低
我有一个代码: $("#button").click(function () { for (var i = 0; i < 4; i++) {
我们的应用程序正在被翻译成多种语言,我们需要一个组合框来列出可能的语言。我们希望使用该语言的语言名称(例如,Français 表示法语)。 列出这些语言是否有任何“正确”的顺序?我们是否根据他们的
我正在尝试在 Ubuntu 12.04 中使用 Tramp/Emacs-23 来编辑远程主机文件。我的远程主机有两步验证 (RSA+Passwd)。我通过 .ssh/config 使用多路复用来确保
我是一名优秀的程序员,十分优秀!