Scala actor 如何返回一个值来响应消息?

发布于 2024-09-01 18:32:35 字数 412 浏览 3 评论 0原文

有很多参与者向发件人回复另一条消息的示例,但在浏览 API 文档时,我注意到!!和 !?属于 CanReply 特征一部分的运算符(这似乎是 2.8 中的新增内容: http://www.scala-lang.org/archives/rc-api/scala/actors/CanReply.html)。因此,我想知道这是否只是让接收/反应块返回一个值的情况,即使 PartialFunction 返回类型不是 Unit 的类型?

我将开始深入研究源代码,尝试弄清楚它们的用途,但如果有人有任何见解或知道任何更深入的文档或示例,那么我将不胜感激。

干杯, 保罗.

There are plenty of examples of actors replying with another message back to the sender, but whilst browsing the API docs I noticed the !! and !? operators which are part of the CanReply trait (which seems to be new to 2.8: http://www.scala-lang.org/archives/rc-api/scala/actors/CanReply.html). I was therefore wondering whether it was just a case of having the receive/react block return a value, i.e. make the PartialFunction return type something other than Unit?

I'll start digging through the source to try to work out how they're meant to be used, but if anyone has any insight or knows of any more in-depth documentation or examples then I'd be most grateful.

Cheers,
Paul.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在你怀里撒娇 2024-09-08 18:32:36

可以使用 reply 方法发送回复,如下所示:

import scala.actors._
class Reverser extends Actor {
  def act() { Actor.loop { react {
    case s: String => Thread.sleep(1000); reply(s.reverse)
    case _ => exit()
  }}}
}

可以通过三种方式显式接受回复。

  • 使用 !!,它返回一个 Future,它是一个容器类,承诺在您需要时为您提供内容。它立即返回,但如果您实际请求内容,则必须等到另一个线程完成并填充请求。
  • 使用 !? 没有超时。只要其他线程需要回复,您的代码就会暂停。
  • 使用带有超时的 !?。您的代码将暂停,直到收到回复或超时到期(以先到者为准)。

这是所有三个的示例:

val r = new Reverser
r.start
val a = (r !! "Hi")
a() match {
  case s: String => println(s)
  case _ => println("Error A")
}
val b = r !? "Hello"
b match {
  case s: String => println(s)
  case _ => println("Error B")
}
val c = (r !? (500,"Howdy"))
c match {
  case Some(s: String) => println(s)
  case Some(_) => println("Error C")
  case None => println("Too slow!")
}
r ! None  // None isn't a string, so r will stop running

如果你运行这个,你会得到

iH
elloH
Too slow!

Replies can be sent with the method reply, as shown here:

import scala.actors._
class Reverser extends Actor {
  def act() { Actor.loop { react {
    case s: String => Thread.sleep(1000); reply(s.reverse)
    case _ => exit()
  }}}
}

There are three ways to explicitly accept the reply.

  • Use !!, which returns a Future, which is a container class that promises to give you the contents when you need them. It returns immediately, but if you actually ask for the contents, you have to wait until the other thread is done and fills the request.
  • Use !? without a timeout. Your code will pause for as long as it takes for the other thread to reply.
  • Use !? with a timeout. Your code will pause until it gets a reply or until the timeout expires, whichever comes first.

Here's an example of all three:

val r = new Reverser
r.start
val a = (r !! "Hi")
a() match {
  case s: String => println(s)
  case _ => println("Error A")
}
val b = r !? "Hello"
b match {
  case s: String => println(s)
  case _ => println("Error B")
}
val c = (r !? (500,"Howdy"))
c match {
  case Some(s: String) => println(s)
  case Some(_) => println("Error C")
  case None => println("Too slow!")
}
r ! None  // None isn't a string, so r will stop running

And if you run this you get

iH
elloH
Too slow!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文