scala Akka Kill vs. Stop vs. Poison Pill?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13847963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Akka Kill vs. Stop vs. Poison Pill?
提问by LaloInDublin
Newbie question of Akka - I'm reading over Akka Essentials, could someone please explain the difference between Akka Stop/Poison Pill vs. Kill ? The book offers just a small explaination "Kill is synchronous vs. Poison pill is asynchronous." But in what way? Does the calling actor thread lock during this time? Are the children actors notified during kill, post-stop envoked, etc? Example uses of one concept vs. the other?
Akka 的新手问题 - 我正在阅读 Akka Essentials,有人可以解释 Akka Stop/Poison Pill 与 Kill 之间的区别吗?这本书只提供了一个小小的解释“杀戮是同步的,而毒丸是异步的。” 但以什么方式?在此期间调用actor 线程是否锁定?儿童演员是否在杀戮、停止后召唤等过程中得到通知?一个概念与另一个概念的示例用法?
Many thanks!
非常感谢!
回答by rs_atl
Both stopand PoisonPillwill terminate the actor and stop the message queue. They will cause the actor to cease processing messages, send a stop call to all its children, wait for them to terminate, then call its postStophook. All further messages are sent to the dead letters mailbox.
双方stop并PoisonPill会终止演员和停止消息队列。它们将导致 actor 停止处理消息,向其所有子进程发送停止调用,等待它们终止,然后调用其postStop钩子。所有进一步的消息都发送到死信邮箱。
The difference is in which messages get processed before this sequence starts. In the case of the stopcall, the message currently being processed is completed first, with all others discarded. When sending a PoisonPill, this is simply another message in the queue, so the sequence will start when the PoisonPillis received. All messages that are ahead of it in the queue will be processed first.
不同之处在于在此序列开始之前处理哪些消息。在stop调用的情况下,当前正在处理的消息首先完成,所有其他消息都被丢弃。发送 a 时PoisonPill,这只是队列中的另一条消息,因此序列将在PoisonPill收到时开始。队列中在它前面的所有消息将首先被处理。
By contrast, the Killmessage causes the actor to throw an ActorKilledExceptionwhich gets handled using the normal supervisor mechanism. So the behaviour here depends on what you've defined in your supervisor strategy. The default is to stop the actor. But the mailbox persists, so when the actor restarts it will still have the old messages except for the one that caused the failure.
相比之下,该Kill消息导致actor 抛出一个ActorKilledException使用正常监督机制处理的 。因此,此处的行为取决于您在主管策略中定义的内容。默认是停止actor。但是邮箱仍然存在,因此当 actor 重新启动时,它仍然会保留旧消息,除了导致失败的消息。
Also see the 'Stopping an Actor', 'Killing an Actor' section in the docs:
另请参阅文档中的“停止演员”、“杀死演员”部分:
http://doc.akka.io/docs/akka/snapshot/scala/actors.html
http://doc.akka.io/docs/akka/snapshot/scala/actors.html
And more on supervision strategies:
以及更多关于监督策略的信息:
http://doc.akka.io/docs/akka/snapshot/scala/fault-tolerance.html
http://doc.akka.io/docs/akka/snapshot/scala/fault-tolerance.html
回答by JohnUK
Use PoisonPill whenever you can. It is put on the mailbox and is consumed like any other message. You can also use "context.stop(self)" from within an actor.
尽可能使用 PoisonPill。它被放在邮箱上并像任何其他消息一样被消费。您还可以在 actor 中使用“context.stop(self)”。
回答by jay
You can use both actor stop and poison pill to stop processing of actors, and kill to terminate the actor in its entirety. x.stop is a call you make in akka receive method, will only replace actor state with new actor after calling postStop. x ! PoisonPill is a method that you pass to actor to stop processing when the actor is running(Recommended). will also replace actor state after calling postStop. x.kill will terminate the actor and will remove the actor in the actor Path and replace the entire actor with a new actor.
您可以使用 actor stop 和毒丸来停止对 actor 的处理,并使用 kill 来完全终止 actor。x.stop 是你在 akka receive 方法中的调用,只会在调用 postStop 后用新的 actor 替换 actor 状态。X !PoisonPill 是一种在actor 运行时传递给actor 以停止处理的方法(推荐)。也将在调用 postStop 后替换 actor 状态。x.kill 将终止actor 并移除actor Path 中的actor 并用一个新actor 替换整个actor。
回答by idonnie
PoisonPill asynchronously stops the actor after it's done with all messages that were received into mailbox, prior to PoisonPill.
PoisonPill 在处理完所有接收到邮箱中的消息后,在 PoisonPill 之前异步停止 actor。

