如何在 Scala actor 内发送延迟响应

发布于 2024-11-03 04:19:57 字数 242 浏览 1 评论 0原文

非参与者类向参与者发送同步消息,如下所示:


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 技术交流群。

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

发布评论

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

评论(3

小情绪 2024-11-10 04:19:57

对当前调用者的引用存储在sender 中。 是完全有效的。

receive {
  case "hello" => reply("world")
}

例如,替换为

receive {
  case "hello" => sender ! "world"
}

您可以将此引用存储在可变变量中,或者递归地通过参与者的循环。

Reference to the current caller is stored at sender. E.g., it's perfectly valid to replace

receive {
  case "hello" => reply("world")
}

with

receive {
  case "hello" => sender ! "world"
}

You can store this ref hereafter in the mutable variable, or pass recursively through the actor's loop.

残花月 2024-11-10 04:19:57

我通常存储发件人参考以供以后使用。

receive {
  case "hello" => 
    val otherParty = sender
    // more receives, etc
    // ...
    otherParty ! "world"   
}

I generally store the sender reference to use it later.

receive {
  case "hello" => 
    val otherParty = sender
    // more receives, etc
    // ...
    otherParty ! "world"   
}
赤濁 2024-11-10 04:19:57

只生成一个匿名参与者来处理消息并在消息准备好时做出响应怎么样?这样接收者就充当了调度者的角色。这不需要可变变量来存储任何内容,因为您在这里使用的是闭包。

import scala.actors.Actor                                                              
import scala.actors.Actor._

case class Message(msg: String)

class MyReceiver extends Actor {
  def act() {
    react {
      case Message(msg) =>
        actor {
          sender ! process(msg)
        }   
    }   
  }

  def process(msg: String): String =
    "Result: " + msg 
}

object Main {
  def main(args: Array[String]) {
    val a = new MyReceiver
    a.start()

    val res = a !? Message("foo")
    println(res)
  }
}

问候,雷丘

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.

import scala.actors.Actor                                                              
import scala.actors.Actor._

case class Message(msg: String)

class MyReceiver extends Actor {
  def act() {
    react {
      case Message(msg) =>
        actor {
          sender ! process(msg)
        }   
    }   
  }

  def process(msg: String): String =
    "Result: " + msg 
}

object Main {
  def main(args: Array[String]) {
    val a = new MyReceiver
    a.start()

    val res = a !? Message("foo")
    println(res)
  }
}

Regards, raichoo

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