如何在 Scala actor 内发送延迟响应
非参与者类向参与者发送同步消息,如下所示:
val response = WorkData !? "hello"
如果我想立即响应此消息,那么我会这样做:
receive {
case "hello" => reply("world")
}
但如果我需要在将来某个时间回复,那么 我如何存储呼叫者参考,然后发送回复?
Non-actor class sends a synchronous message to actor like this:
val response = WorkData !? "hello"
If i want to respond to this message right away, than i will do this:
receive {
case "hello" => reply("world")
}
But if i need to reply some time in the future, than
how do i store the caller reference, and send the reply after that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对当前调用者的引用存储在
sender
中。 是完全有效的。例如,替换为
您可以将此引用存储在可变变量中,或者递归地通过参与者的循环。
Reference to the current caller is stored at
sender
. E.g., it's perfectly valid to replacewith
You can store this ref hereafter in the mutable variable, or pass recursively through the actor's loop.
我通常存储发件人参考以供以后使用。
I generally store the sender reference to use it later.
只生成一个匿名参与者来处理消息并在消息准备好时做出响应怎么样?这样接收者就充当了调度者的角色。这不需要可变变量来存储任何内容,因为您在这里使用的是闭包。
问候,雷丘
How about just spawning an anonymous actor that processes the message and respond when it's ready? This way the receiver just acts as a dispatcher. This does not need a mutable variable to store anything since you are using a closure here.
Regards, raichoo