RemoteActor.select - 结果确定吗?
我想知道调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题让我很好奇,所以我快速查看了源代码...这就是我的发现:
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).