Scala,检查Actor是否已退出 [英] Scala, check if Actor has exited

查看:121
本文介绍了Scala,检查Actor是否已退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scala 2.8中启动演员时,可以通过消息传递进行交流。反过来,这意味着我可以发送最终的Exit()消息或任何我认为适合我的协议的消息。



但是我如何检查演员是否退出了?我可以很容易地想象自己有一个任务,即主演员先启动一些工人演员,然后简单地等待答案,每次检查这是否是最终答案(即是否有演员仍在工作或他们都退出了?)。



我当然可以让他们都发回我完成了的消息,然后对它们进行计数,但这在某种程度上并不令人满意。



测试工作者角色完成时的最佳实践是什么?



EDIT#1



我正在研究期货,但遇到了麻烦。有人可以解释为什么此代码不起作用:

  package test 
import scala.actors.Futures._

对象FibFut扩展应用程序{

def fib(i:Int):Int =
if(i <2)
1
else
fib(i-1)+ fib(i-2)

val f =未来{fib(3)}

println(f())

}

如果我在future-body内定义函数fib,它就可以工作。它一定是作用域的东西,但是上面我没有任何错误,只是挂起了。有人吗?



EDIT#2



它似乎扩展Application不是一个好方法。定义主要方法可使一切正常。下面的代码正是我想要的,所以未来表示赞赏:)

  package测试

进口scala.actors.Futures._

对象FibFut {

def fib(i:Int):Int = if(i< ; 2)1 else fib(i-1)+ fib(i-2)

def main(args:Array [String]){

val fibs = for( i<-0至50)的收益({fub(i)}

for(未来<-fibs)println(future())

}

}


解决方案

我是粉丝就我完成了消息而言;这是管理工作分配的好方法,而且,作为奖励,您已经知道所有孩子何时都完成了自己的工作。



但是,如果您真的只是想要要一次进行一些工作并等待一切准备就绪,请查看 scala.actors.Futures 。您可以要求它进行一些计算:

  val futureA = Futures.future {
val a = veryExpensiveOperation
(a,我来自未来!)
}

那么如果您有多个请求,则可以等待所有操作完成:

  Futures.awaitAll(600 * 1000,futureA, futureB,futureC,futureD)
//一旦计算完所有AD,就返回
val actualA = futureA()//现在我们得到值


in Scala 2.8 when I start actors, I can communicate via message passing. This in turn means that I can send the ultimate Exit() message or whatever I decide fits my protocol.

But how will I check if an actor has exited? I can easily imagine myself having a task where a master actor starts some worker actors and then simply waits for the answers, each time checking if this was the final answer (i.e. are any Actors still working or did they all exit?).

Of course I can let them all send back an "I'm done" message, and then count them, but this is somehow unsatisfactory.

What is best practise when testing for the completion of worker-actors?

EDIT#1

Hey guys, I'm looking into Futures, but having trouble. Can someone explain why this code doesn't work:

package test
import scala.actors.Futures._

object FibFut extends Application{

    def fib(i:Int):Int = 
        if(i<2)
            1
        else
            fib(i-1)+fib(i-2)

    val f = future{ fib(3) }

    println(f())    

}

It works if I define the function fib inside the future-body. It must be a scope thing, but I don't get any errors with the above, it simply hangs. Anyone?

EDIT#2

It seems that extending Application wasn't a nice way to go. Defining a main method made everything work. The below code is what I was looking for, so Futures get the thumbs up :)

package test

import scala.actors.Futures._

object FibFut {

  def fib(i: Int): Int = if (i < 2) 1 else fib(i - 1) + fib(i - 2)

  def main(args: Array[String]) {

    val fibs = for (i <- 0 to 50) yield future { fib(i) }

    for (future <- fibs) println(future())

  }

}

解决方案

I'm a fan of "I'm done" messages, personally; it's a good way to manage distribution of work, and as a bonus, you already know when all children have finished what they're doing.

But if you really just want to farm out some work once and wait until everything is ready, check out scala.actors.Futures. You can ask it to do some computation:

val futureA = Futures.future {
  val a = veryExpensiveOperation
  (a,"I'm from the future!")
}

and then you can wait for everything to complete, if you have made multiple requests:

Futures.awaitAll(600*1000, futureA, futureB, futureC, futureD)
// Returns once all of A-D have been computed
val actualA = futureA()   // Now we get the value

这篇关于Scala,检查Actor是否已退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆