简体   繁体   English

在erlang OTP中重启孩子时,如何向主管提供额外信息?

[英]How to provide extra information to supervisor when restarting a child in erlang OTP?

Say I have a supervisor sup supervise some workers, when a worker started, it register itself with some name, like worker_1 , worker_2 , etc.. When worker_N crashed and get restarted by sup , how to register the newly created process as worker_N , ie, how to let the name of a worker survive a restart? 假设我有一个主管sup监督一些工人,当一个工人启动时,它使用一些名称注册自己,例如worker_1worker_2等。当worker_N崩溃并被sup重新启动时,如何将新创建的进程注册为worker_N ,即,如何让一个工人的名字在重启后幸免于难?

How to restart child with custom state using Erlang OTP supervisor behaviour? 如何使用Erlang OTP主管行为以自定义状态重新启动子级? gives no answer, since I only want the name, there maybe a solution. 没有给出答案,因为我只想要名称,所以可能有解决方案。

You could have the worker register itself when it is started by having it call register(Name, self()) in init and passing Name as an argument there. 您可以通过启动init调用register(Name, self())并在其中传递Name作为参数来启动worker自身。 This is global across the node, so it doesn't matter what process does the registering. 这在整个节点上都是全局的,因此注册的过程无关紧要。

BUT

It looks like you are dynamically creating atoms because you want to use registered processes everywhere. 您似乎正在动态创建原子,因为您想在所有地方使用注册的进程。 This is a very common mistake -- but there is hope! 这是一个非常常见的错误-但有希望!

Instead of dynamically creating atoms to name every child worker in existence, consider a few things first: 与其动态创建原子来命名现有的每个童工,不如先考虑以下几点:

  • You don't need them all named. 您不需要全部命名。 Workers have a limited job description (and if they don't you have bigger problems) and they are nearly always tied to their resources, their caller or their spawner, who already knows them. 工人的工作描述有限(如果您不这样做,您会遇到更大的问题),并且他们几乎总是与已经了解他们的资源,呼叫者或产生者联系在一起。
  • You do need them named (but really, you probably don't) because they represent concrete elements of a large system, like a location in a game, a channel in chat, your stalking victim's Twitter feed, etc. Name them a different way, by keeping a digest of [{Key, Value}] where [{AnyTerm, Pid}] . 您确实需要为它们命名(但实际上可能没有),因为它们代表了大型系统的具体元素,例如游戏中的位置,聊天中的频道,跟踪的受害者的Twitter feed等。以不同的方式命名它们,通过保留[{Key, Value}]的摘要,其中[{AnyTerm, Pid}] This lets you know more about the process by looking at its name and keeps the global spaces of your system free of private worker detritus. 通过查看其名称可以使您了解有关该过程的更多信息并使系统的全局空间不受私人工作者的破坏。

There's more... 还有更多...

If you are naming processes integers you are either making the same mistake countless web developers make when using relational databases (making everything have an integer primary key), or you are dealing with workers that don't need names . 如果您要为整数命名过程,则可能是由于使用关系数据库(使所有内容都具有整数主键)而使无数Web开发人员犯同样的错误,或者正在处理不需要名称的工作人员。

If you actually do need them named (it happens), consider using the global module (it lets you name any process any term you want, and the names are usable across all nodes -- magic!), "process groups" in the pg2 module, or gproc (a library written to handle this exact problem, and also lets you use any term you want to name processes). 如果确实需要命名它们(发生),请考虑使用global模块(它可以让您为所需的任何进程命名任何进程,并且该名称可在所有节点上使用-魔术!), pg2 “进程组”模块或gproc (为处理此确切问题而编写的库,还允许您使用要为进程命名的任何术语)。

A personal note 个人笔记

If you ever find yourself in a situation where generating dynamic atoms seems like a good idea (for whatever reason) you should always stop whatever you are doing and think very hard about what you are doing. 如果您发现处于产生动态原子似乎是个好主意的情况下(无论出于何种原因),则应始终停止所做的任何事情,并认真思考所做的事情。 This is because generating atoms is a strong indication that you're doing it wrong . 这是因为生成原子很容易说明您做错了 Try to discover why you are fighting the system so hard -- Erlang isn't meant to work that way. 尝试找出为什么要如此努力地与系统抗争-Erlang并非以这种方式工作。

In every case I've either encountered in my own code or witnessed in the work of others, this sort of thing is a sign that a cleaner, more Erlangy way of doing things exists, and everything just gets easier if I discover whatever that way is instead of adding more layers to the big knot of tangled logic I'm working on. 在我自己的代码中遇到的所有情况或在他人的工作中目睹的每种情况下,这种情况都表明存在更干净,更Erlangy的工作方式,并且只要我发现这种方式,一切都会变得更加容易而不是在我正在研究的纠结逻辑的大结中增加更多的层次。 The good news is that accumulating Erlang idioms is quick and they are hard to forget because they usually make so much sense in retrospect (nearly always you find you were reinventing the wheel before discovering the accepted solution -- and wind up with a "ah, great minds think alike" feeling at the end). 好消息是,累积Erlang习惯用法很快,而且很难忘记,因为通常回想起来它们是很有意义的(几乎总会发现,在发现可接受的解决方案之前,您正在重新发明轮子-并以“啊,伟大的思想都一样”。

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

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