RemoteActor.select - 结果确定吗?

发布于 2024-09-10 11:44:23 字数 1715 浏览 6 评论 0原文

我想知道调用 val delegate = RemoteActor.select() 时是否存在确定性。 我问这个问题是因为我注意到当我通过网络发送代表时程序不会终止。

是否有任何其他副作用(取决于代表)?

当 RemoteActor.select 将为相同的参数返回相同的委托时,是否有任何规则?

下面是一些演示 RemoteActor.select 问题的示例代码:

package test

import scala.actors.Actor, Actor._
import scala.actors.remote._, RemoteActor._

object DelegateTest {
  def main(args :Array[String]) {
    val actor = new Actorlein("localhost", 63000, 'first)
    actor ! 'eval
  }
}

class Actorlein(val host: String, val port: Int, val symbol: Symbol) extends Actor {
  val preProxy1 = select(node, symbol)
  val preProxy2 = select(node, symbol)

  val node = Node(host,port)
  this.start
  alive(port)
  register(symbol, this)

  val initProxy1 = select(node, symbol)
  val initProxy2 = select(node, symbol)

  override def act = { 
    val actProxy1 = select(node, symbol)
    val actProxy2 = select(node, symbol)
    react {
      case 'eval => {
        val reactProxy1 = select(node, symbol)
        val reactProxy2 = select(node, symbol)
        //all true
        println("pProxy equal? "+(preProxy1 == preProxy2))
        println("iProxy equal? "+(initProxy1 == initProxy2))
        println("aProxy equal? "+(actProxy1 == actProxy2))
        println("rProxy equal? "+(reactProxy1 == reactProxy2))
        //all true()
        println("i equal p? "+(initProxy1 == preProxy1)) //false
        println("r equal p? "+(reactProxy1 == preProxy1))//false
        println("a equal p? "+(actProxy1 == preProxy1))  //false
        println("i equal a? "+(initProxy1 == actProxy1)) //false
        println("r equal a? "+(reactProxy1 == actProxy1))//true
      }
      case any => println("Unkown Msg: "+any)
    } 
  }
}

I wonder if there is any determinism when calling val delegate = RemoteActor.select().
I'm asking this, because I noticed that the program doesn't terminate, when I'm sending delegates over the net.

Are there any other side effects, which depend on the delegate?

Are there any rules, when RemoteActor.select will return the same delegate for the same arguments?

Here's some example code which demonstrates the RemoteActor.select problem:

package test

import scala.actors.Actor, Actor._
import scala.actors.remote._, RemoteActor._

object DelegateTest {
  def main(args :Array[String]) {
    val actor = new Actorlein("localhost", 63000, 'first)
    actor ! 'eval
  }
}

class Actorlein(val host: String, val port: Int, val symbol: Symbol) extends Actor {
  val preProxy1 = select(node, symbol)
  val preProxy2 = select(node, symbol)

  val node = Node(host,port)
  this.start
  alive(port)
  register(symbol, this)

  val initProxy1 = select(node, symbol)
  val initProxy2 = select(node, symbol)

  override def act = { 
    val actProxy1 = select(node, symbol)
    val actProxy2 = select(node, symbol)
    react {
      case 'eval => {
        val reactProxy1 = select(node, symbol)
        val reactProxy2 = select(node, symbol)
        //all true
        println("pProxy equal? "+(preProxy1 == preProxy2))
        println("iProxy equal? "+(initProxy1 == initProxy2))
        println("aProxy equal? "+(actProxy1 == actProxy2))
        println("rProxy equal? "+(reactProxy1 == reactProxy2))
        //all true()
        println("i equal p? "+(initProxy1 == preProxy1)) //false
        println("r equal p? "+(reactProxy1 == preProxy1))//false
        println("a equal p? "+(actProxy1 == preProxy1))  //false
        println("i equal a? "+(initProxy1 == actProxy1)) //false
        println("r equal a? "+(reactProxy1 == actProxy1))//true
      }
      case any => println("Unkown Msg: "+any)
    } 
  }
}

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

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

发布评论

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

评论(1

暖伴 2024-09-17 11:44:23

你的问题让我很好奇,所以我快速查看了源代码...这就是我的发现:

select 返回的内容似乎取决于处理 TCP 连接的对象。由于此 NetKernel 会记住先前创建的代理,因此只要“当前 Netkernel”相同,代理就相同。当前的 Netkernel 取决于 Actor.self 的当前值,这可能(我没有深入研究)当前线程。对我来说,这解释了为什么 r=a 但 p 和 i 与此不同。

我猜想, p 和 i 不同的原因是新的 NetKernel 通过 alive(port) 调用与 actor 关联(actor 的内核需要使用指定的端口,而不是随机端口)。

Your question got me curious so I had a quick look at the source ... here's what I found:

What select returns seems to depend on an object that handles the TCP connections. As this NetKernel remembers previously created proxies, the proxies are the same as long as the "current Netkernel" is the same. The current Netkernel depends on the current value of Actor.self, which might (I didn't dig that deep) on the current thread. For me that explains why r=a but p and i are different from that.

I guess, the reason for p and i being different is that a new NetKernel is associated with the actor by the alive(port) call (the actor's kernel needs to use the specified port, and not a random one).

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