重新启动erlang进程并保存状态 [英] Restarting erlang process and preserving state

查看:421
本文介绍了重新启动erlang进程并保存状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主管进程启动子进程数。当孩子去世时,我用新的Pid产生一个新的过程。这意味着我放弃刚刚死亡的我的子进程的状态信息。我希望我的客户端使用始终相同的标识符与子进程通信。尽管子进程可能会死亡,并由主管重新启动。



我正在考虑使用唯一的名称注册子进程,并将子状态存储在ets表中。问题是 - 在Erlang中接受这个问题的推荐方法是什么?



谢谢!

解决方案

在ets表中存储进程状态将有助于在崩溃之间保持状态,我通常使用全局注册表给予进程持久的名称。 (播放器200将被注册为{player,200})。我不建议使用本地注册表,因为它要求您使用原子,如果您有很多子进程,您可以匆忙地嚼掉你的原子极限动态创建它们(如player_200,player_201等)



尽管在ets表中存储子状态有其自身的风险和问题。如果一个孩子在发生错误的时刻和保存到ets表之间崩溃,你应该是好的。但是,如果处理导致孩子保存垃圾状态的数据,那么在处理下一条消息时崩溃?您将重新启动该过程,从ets表中加载不良状态,并再次崩溃您的下一个消息。有一些方法来处理这个问题,但你应该意识到这是一个可能性,并且可以解决。



虽然Erlang隐藏了分发ets表的问题所有的流程都是以CPU和潜在的争议为代价的。如果您在ets表中进行了很多更改,那么您将在演出中付出代价。



如果您的孩子正在崩溃,不应该寻找一种方式来消除错误的条件呢?我通常会将进程崩溃作为我需要根本原因和修复的东西。 ?


I have a supervisor process which starts number of child processes. Currently when the child dies I spawn a new process with new Pid. This means I loose the state information of my child process which has just died. I want my clients to communicate with child processes using always the same identifier. Despite the fact that child process may die and be restarted by the supervisor.

I was thinking of registering child processes with unique names and storing child state in ets table. The question is - what is the recommended way of approaching such problem in Erlang?

Thanks!

解决方案

Storing process state in an ets table would work for keeping your state around between crashes, and I usually use the global registry for giving processes persistent names. (Player 200 would be registered as {player, 200}.) I don't recommend using the local registry because it requires that you use atoms and if you have many child processes, you can chew up your limit of atoms in a hurry by creating them dynamically (like player_200, player_201, etc.)

Storing child state in the ets table has its own risks and issues, though. If a child crashes between the moment when an error occurs and when it saves to the ets table, you should be alright. However, what if you process data that causes the child to save garbage state, then crash on processing the next message? You'll restart the process, load the bad state from the ets table, and crash on your next message again. There are certainly ways to deal with this, but you should be aware that it is a possibility and work around it.

While Erlang hides the problems of distributing an ets table to all processes, it does so at the cost of CPU and potential contentions. If you're pushing a lot of changes to your ets table, you're going to pay for it in performance.

If your children are crashing, shouldn't you be looking for a way for them to remove the erroneous conditions, anyway? I would usually take a process crash as something that I needed to root cause and fix. ?

这篇关于重新启动erlang进程并保存状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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