我有一个主管,他启动了 simple_one_for_one 孩子。每个孩子实际上都是一个主管,有自己的树。每个孩子一开始都有一个唯一的ID,这样我就可以区分他们。然后,每个 gen_server 都使用 start_link(Id) 启动,其中:
-define(SERVER(Id), {global, {Id, ?MODULE}}).
start_link(Id) ->
gen_server:start_link(?SERVER(Id), ?MODULE, [Id], []).
因此,每个 gen_server 都可以轻松地使用 {global, {Id, module_name}} 进行寻址。
现在我想将这个儿童主管纳入应用程序。所以,我的母亲主管应该代替主管来开始申请。这应该很简单,除了一个部分:将 ID 传递给应用程序。使用 ID 启动 Supervisor 很简单:supervisor:start_child(?SERVER, [Id])。我该如何进行申请?如何使用不同的 ID 启动多个同名的应用程序(以便我可以访问相同的 .app 文件)(以便我可以使用supervisor:start_child(?SERVER, [Id]) 启动我的孩子)?
如果我的问题不够清楚,这是我的代码。因此,目前, es_simulator_dispatcher 启动 es_simulator_sup。我想要这样: es_simulator_dispatcher 启动 es_simulator_app ,它启动 es_simulator_sup 。这就是全部内容了:-)
提前致谢,
迪杰斯特拉
I have a supervisor which starts simple_one_for_one children. Each child is in fact a supervisor which has its own tree. Each child is started with an unique ID, so I can distinguish them. Each gen_server is then started with start_link(Id), where:
-define(SERVER(Id), {global, {Id, ?MODULE}}).
start_link(Id) ->
gen_server:start_link(?SERVER(Id), ?MODULE, [Id], []).
So, each gen_server can easily be addresed with {global, {Id, module_name}}.
Now I'd like to make this child supervisor into application. So, my mother supervisor should start applications instead of supervisors. That should be straightforward, except one part: passing ID to an application. Starting supervisor with an ID is easy: supervisor:start_child(?SERVER, [Id]). How do I do it for application? How can I start several applications of the same name (so I can access the same .app file) with different ID (so I can start my children with supervisor:start_child(?SERVER, [Id]))?
If my question is not clear enough, here is my code. So, currently, es_simulator_dispatcher starts es_simulator_sup. I'd like to have this: es_simulator_dispatcher starts es_simulator_app which starts es_simulator_sup. That's all there is to it :-)
Thanks in advance,
dijxtra
发布评论
评论(2)
我认为应用程序并不像您想要的那样用于动态构建。我会创建一个应用程序,因为在 Erlang 中,应用程序更多的是代码包,而不是运行进程的包(你可以说它们更多的是编译时的产物,而不是运行时的产物)。
通常,您通过内置配置系统将配置提供给应用程序。也就是说,您使用
application:get_env(Key)
来读取它应该使用的内容。还有一个application:set_env(...)
将特定配置提供给其中 - 但首选方法是磁盘上的配置文件。这对于您的情况可能有效,也可能无效。从某种意义上说,您要做的相当于创建 200 个 Apache 配置文件,然后生成 200 个彼此相邻的 Apache 系统,而不是运行单个系统然后处理其中的多个域。
I don't think applications where meant for dynamic construction like you want. I'd make a single application, because in Erlang, applications are bundles of code more than they are bundles of running processes (you can say they are an artifact of compile-time moreso than of runtime).
Usually you feed configuration to an application through the built-in configuration system. That is, you use
application:get_env(Key)
to read something it should use. There is also anapplication:set_env(...)
to feed specific configuration into one - but the preferred way is the config file on disk. This may or may not work in your case.In some sense, what you are up to corresponds to creating 200 Apache configuration files and then spawn 200 Apache systems next to each other, rather than running a single one and then handle the multiple domains inside it.
应用程序不在其他任何东西下运行,它们是顶级抽象。当您使用
application:start/1
启动应用程序时,该应用程序由管理应用程序的应用程序控制器启动。应用程序包含代码和数据,并且可能在运行时包含在运行时执行应用程序操作的进程的监督树。由于应用程序的性质,运行应用程序的多个调用实际上没有意义。我建议阅读OTP 设计原则用户指南,了解 OTP 组件的描述、它们如何关联以及如何使用它们。
Applications don't run under anything else, they are a top-level abstraction. When you start an application with
application:start/1
the application is started by the application controller which manages applications. Applications contain code and data, and maybe at runtime a supervision tree of processes doing the applications thing at runtime. Running multiple invocations of an application does not really make sense because of the nature of applications.I would suggest reading OTP Design Principles User's Guide for a description of the components of OTP, how they relate and how they are intended to be used.