问题描述
我有工作进程需要收集/计算数据作为参数启动。这样就可以重新启动了。我应该在哪里放置初始化代码?里面的主管init?还是在模块里面的start_link或init?在Erlang中有什么最佳做法吗?如果gen_server组件具有关键状态,或状态不能重新计算/重新收集,我通常避免在gen_server本身保持状态。我选择在外部进程/ ets表中维护状态。如果您正在执行此方法,请确保ets表是由externel进程创建的(您确定不会死),例如,应用程序进程 - 或者 - 在init方法中创建ets表gen_server并使用ets:give_away / 3方法将其移交给外部进程(当然,您需要检查表中是否已在gen_server的init方法中创建)。否则,ets表将被销毁当进程死机时。
I have worker processes that needs gathered/calculated data as arguments on start up. This is then needed on re-starts as well. Where should I put the initialization code? Inside the supervisors init? Or inside the modules start_link, or init? Are there any best practices in Erlang when it comes to this?
If the gen_server component has critical state, or state which cannot be re-calculated/re-gathered, I generally avoid keeping the state in gen_server itself. I instead choose to maintain state in an external process/ets table. If you are going by this approach, make sure the ets table is either created by an externel process (which you are sure will not die), for eg., the application process -or- create the ets table in the init method of the gen_server and use the "ets:give_away/3" method to hand it off to an external process (of course, you would need to check if the table is already created in the gen_server's init method).. Else the ets table will be destroyed when the process dies..
这篇关于工作进程死机时的数据持久性如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!