如何将应用程序作为依赖项使用两次

时间:2017-04-27 05:41:41

标签: elixir otp

Elixir / Erlang application可能有依赖的应用程序。正如文档:

  

您可以配置生成的   应用程序通过在mix.exs中定义应用程序/ 0函数   以下选项:

• :applications - all applications your application depends on at
  runtime. By default, this list is automatically inferred from your
  dependencies. Any extra Erlang/Elixir dependency must be specified in
  :extra_applications. Mix and other tools use the application list in order
  to start your dependencies before starting the application itself.
• :extra_applications - a list of Erlang/Elixir applications that you
  want started before your application. For example, Elixir's :logger or
  Erlang's :crypto.

这些相关的应用程序将在启动我的应用程序之前全部启动。到目前为止一切都很好。

applications键需要一个原子列表,它不允许元组。这使得无法将参数传递给Application.start/2

有没有可靠的方法将start_args传递给相关应用,还是我被迫使用手动MyApp.start(:normal, [:hello])调整默认行为?

如果答案是“是”,我怎样才能使用不同的start_args列表启动相同的依赖应用程序两次

1 个答案:

答案 0 :(得分:2)

简短的回答是否定的。 Elixir并非所有相同的应用程序都要多次启动。但是,你可能会有一些技巧。

通过将app: false添加到deps,该应用将不会自动启动。

{:my_dep, "...", app: false}

然后你可以从主应用程序启动它。

MyDep.Application.start(...)

然后,您可以在主管的早期启动应用程序中的其他工作人员。

您需要在应用中注意一些事项。

  • 应用程序是否使用命名进程?那将是一个问题。
  • 应用程序是否具有全局配置?这可能是个问题。

但所有这些都取决于您尝试使用的依赖关系。

相关问题