Akka:完成后必须清理动态创建的演员吗? [英] Akka: Cleanup of dynamically created actors necessary when they have finished?

查看:135
本文介绍了Akka:完成后必须清理动态创建的演员吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Akka及其Java API UntypedActor实现了一个Actor系统。在其中,一个actor(类型A)使用 getContext()。actorOf(...); 按需动态启动其他actor(类型B)。那些B演员会做一些A不再关心的计算。但我想知道:是否有必要在他们完成后清理那些B型演员?如果是这样,怎么样?

I have implemented an Actor system using Akka and its Java API UntypedActor. In it, one actor (type A) starts other actors (type B) dynamically on demand, using getContext().actorOf(...);. Those B actors will do some computation which A doesn't really care about anymore. But I'm wondering: is it necessary to clean up those actors of type B when they have finished? If so, how?


  • 让B演员调用 getContext()。stop(getSelf())什么时候完成?

  • 通过让B演员调用 getSelf()。tell(Actors.poisonPill()); 什么时候完成? [这就是我现在正在使用的]。

  • 什么都不做?

  • By ...?

  • By having B actors call getContext().stop(getSelf()) when they're done?
  • By having B actors call getSelf().tell(Actors.poisonPill()); when they're done? [this is what I'm using now].
  • By doing nothing?
  • By ...?

文档不清楚,或者我忽略了它。我有一些Scala的基本知识,但Akka的来源并不完全是入门级的东西...

The docs are not clear on this, or I have overlooked it. I have some basic knowledge of Scala, but the Akka sources aren't exactly entry-level stuff...

推荐答案

你是什么正在描述的是按照请求(在A的上下文中定义)创建的单用途参与者,它处理一系列事件然后完成,对吧?这是绝对正常的,你把它们关闭是正确的:如果你不这样做,它们会随着时间的推移累积而你会遇到内存泄漏。最好的方法是你提到的第一种可能性(最直接),但第二种也没关系。

What you are describing are single-purpose actors created per "request" (defined in the context of A), which handle a sequence of events and then are done, right? That is absolutely fine, and you are right to shut those down: if you don’t, they will accumulate over time and you run into a memory leak. The best way to do this is the first of the possibilities you mention (most direct), but the second is also okay.

一些背景:演员在他们的父母是为了可以识别(例如需要在远程处理,但也在其他地方),这种注册使他们不被垃圾收集。 OTOH,每个父母都有权访问它创建的子节点,因此没有自动终止(即通过Akka)是有意义的,而是要求在用户代码中显式关闭。

A bit of background: actors are registered within their parent in order to be identifyable (e.g. needed in remoting but also in other places) and this registration keeps them from being garbage collected. OTOH, each parent has a right to access the children it created, hence no automatic termination (i.e. by Akka) makes sense, instead requiring explicit shutdown in user code.

这篇关于Akka:完成后必须清理动态创建的演员吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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