简体   繁体   English

由PoisonPill阻止后,Akka演员仍然可用

[英]Akka Actors Still Available After Stopped by PoisonPill

I'm using akka to dynamically create actors and destroy them when they're finished with a particular job. 我正在使用Akka动态创建演员,并在完成特定工作后销毁他们。 I've got a handle on actor creation, however stopping the actors keeps them in memory regardless of how I've terminated them. 我已经掌握了演员创建的方法,但是无论我如何终止演员,停止演员都可以将它们保存在内存中。 Eventually this causes an out of memory exception, despite the fact that I should only have a handful of active actors at any given time. 最终,这将导致内存不足的异常,尽管事实是,在任何给定时间我只应该有几个活跃的演员。

I've used: 我用过:

self.tell(PoisonPill, self)

and: 和:

context.stop(self)

to try and destroy the actors. 试图消灭演员。 Any ideas? 有任何想法吗?

Edit: Here's a bit more to flesh out what I'm trying to do. 编辑:这里还有很多东西可以充实我想做的事情。 The program opens up and spawns ten actors. 该程序将打开并产生十个演员。

val system = ActorSystem("system")
(1 to 10) foreach { x => 
  Entity.count += 1
  system.actorOf(Props[Entity], name = Entity.count.toString())
}

Here's the code for the Entity: 这是实体的代码:

class Entity ()  extends Actor {
  Entity.entities += this
  val id = Entity.count
  import context.dispatcher
  val tick = context.system.scheduler.schedule(0 millis, 100 millis, self, "update")
  def receive = {
    case "update" => {
      Entity.entities.foreach(that => collide(that))
    }
  }
  override def postStop() = tick.cancel()
  def collide(that:Entity) {
    if (!this.isBetterThan(that)) {
      destroyMe()
      spawnNew()
    }        
  }
  def isBetterThan() :Boolean = {
    //computationally intensive logic
  }
  private def destroyMe(){
    Entity.entities.remove(Entity.entities.indexOf(this))
    self.tell(PoisonPill, self)
    //context.stop(self)
  }
  private def spawnNew(){
    val system = ActorSystem("system")
    Entity.count += 1
    system.actorOf(Props[Entity], name = Entity.count.toString())
  }  
}
object Entity {
  val entities = new ListBuffer[Entity]()
  var count = 0
}

Thanks @AmigoNico, you pointed me in the right direction. 谢谢@AmigoNico,您为我指明了正确的方向。 It turns out that neither 事实证明,两者都不

self.tell(PoisonPill, self)

nor 也不

context.stop(self)

worked for timely Actor disposal; 为及时处理演员工作; I switched the line to: 我将线路切换为:

system.stop(self)

and everything works as expected. 一切都按预期进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM