简体   繁体   English

主管和工作进程是否应该注册为全局? -二郎

[英]Should supervisor and worker process be registered as global? -Erlang

我刚开始学习 Erlang 一个月,很想知道在单例进程中是否应该将 supervisor 模块和 worker 模块都注册为全局?

You don't "register" a module as global, you don't register a module at all.您没有将模块“注册”为全局模块,您根本没有注册模块。 If a module is loaded on a node then any other module evaluated in any process on that node can call functions in the original module.如果在节点上加载模块,则在该节点上的任何进程中评估的任何其他模块都可以调用原始模块中的函数。 No registering or importing or anything else necessary.无需注册或导入或任何其他必要的操作。

If you mean registering a process as global then it depends which nodes are are expected to be able to find the process.如果您的意思是将进程注册为全局,那么这取决于预计哪些节点能够找到该进程。 If it is only to be reached from the same node the a local register/2 is enough.如果只能从同一节点到达,则本地register/2就足够了。 If, however, you want it to be globally accessible then you need to register it on all nodes which you typically do using the global module and with the {global,Name} argument when starting supervisors and behaviours.但是,如果您希望它可以全局访问,那么您需要在所有节点上注册它,这些节点通常使用global模块并在启动主管和行为时使用{global,Name}参数。

EDIT:编辑:

OK, the only processes that really needs to be globally registered are the ones that you need to be globally accessible, which in this case is probably only the worker process.好的,唯一真正需要全局注册的进程是您需要全局访问的进程,在这种情况下可能只是工作进程。 The supervisor probably needs only local registering, if any at all.主管可能只需要本地注册,如果有的话。

Note that registering the supervisor, either locally or globally, will NOT by default register the worker process even if it is its child.请注意,在本地或全局注册主管,默认情况下不会注册工作进程,即使它是其子进程。 You have to explicitly register each process.您必须明确注册每个进程。

The rule is simple: you should register the process you are going to communicate with by name.规则很简单:您应该按名称注册要与之通信的进程。

If you are going to communicate with supervisor, then you have to register supervisor.如果您要与主管沟通,那么您必须注册主管。 If you are going to communicate with worker - you should register worker.如果您要与工人沟通 - 您应该注册工人。

In my practice I had to register workers more often, and only had to register supervisors implementing simple_one_for_one strategy that were used as process factories在我的实践中,我不得不更频繁地注册工人,并且只需要注册实施simple_one_for_one策略的主管, simple_one_for_one策略用作流程工厂

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

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