Erlang:何时执行`inets:start()`?

发布于 2024-08-13 11:46:00 字数 260 浏览 5 评论 0原文

执行 inets:start() 的适当位置是什么?

  1. 在“applicationname_app”模块中?
  2. applicationname_sup 管理模块中?
  3. 在从主管挂起的子进程中?\
  4. 在其他地方?

(我仍在与 inets:httpd 作斗争)

注意:答案不能是“使用启动文件”。

What is the appropriate place for performing inets:start() ?

  1. in `applicationname_app' module?
  2. in applicationname_sup supervisor module?
  3. in a child process hanging from the supervisor?\
  4. someplace else?

(I am still struggling with inets:httpd)

Note: the answer cannot be to the tune " use a boot file " , please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆で 2024-08-20 11:46:00

inets 是一个“独立”的 Erlang 应用程序; inets:start() 只是 application:start(inets) 的别名。我想答案很大程度上取决于您如何维护代码。

如果您的代码打包为 应用程序,您的 .app 文件应列出 inets 根据需要在您的之前启动(请参阅应用程序标签)。使用 application:start(my_app). 启动应用程序现在将确保 inets 也启动。结果:如果您创建启动文件,它也会为您启动 inets :-P

如果您热衷于不使用应用程序,我想选择取决于您的代码如何工作。如果您总是需要启动 inets,那么最好由您的任何主管启动。如果很少需要它,您始终可以确保它以如下方式启动:

ensure_app_started(App) ->
  case application:started(App) of
    ok -> ok;
    {error, already_started} -> ok;
    Error -> Error
  end.

inets is a "stand-alone" Erlang application; inets:start() is just an alias to application:start(inets). I guess the answer pretty much depends on how you maintain your code.

If your code is packaged as an application, your .app file should list inets as required to be started before yours (see the applications tag). Starting your applicaion with application:start(my_app). will now ensure that inets is also started. Consequence: if you make a boot file, it will also start inets for you :-P

If you are keen on not using applications, I guess the choice depends on how your code works. If you will always need inets to be started, it is better started by any of your supervisors. If it is rarely needed, you can always make sure it is started with something like:

ensure_app_started(App) ->
  case application:started(App) of
    ok -> ok;
    {error, already_started} -> ok;
    Error -> Error
  end.
妄断弥空 2024-08-20 11:46:00

2019 年,我们使用 rebar3创建应用程序并管理其依赖项。对于需要下载的依赖项,将其添加到 rebar.config 中,rebar3 将下载依赖项。例如,如果您将 hackney (http 客户端)添加到 rebar.config:

{erl_opts, [debug_info]}.
{deps, [
  {hackney, ".*", {git, "git://github.com/benoitc/hackney.git", {branch, "master"}}}
]}.

{shell, [
  % {config, "config/sys.config"},
    {apps, [http_client]}
]}.

然后执行:

../your_app_name$ rebar3 compile

rebar3 将下载 hackney 并编译所有文件在应用程序中。

为了确保所有依赖项在应用程序之前启动,请将依赖项的名称添加到:

src/your_app_name.app.src

例如,

{application, http_client,
 [{description, "An OTP application"},
  {vsn, "0.1.0"},
  {registered, []},
  {mod, {http_client_app, []}},
  {applications,
   [kernel,
    stdlib,
    hackney    %%%<=========HERE
   ]},
  {env,[]},
  {modules, []},

  {licenses, ["Apache 2.0"]},
  {links, []}
 ]}.

实际的 .app 文件在此处创建:

_build/default/lib/your_app_name/ebin/your_app_name.app

要在 shell 中启动应用程序及其所有依赖项,您需要执行以下操作:

../your_app_name$ rebar3 shell 

inets 应用程序随 erlang 一起提供,因此无需下载,因此您无需在 rebar.config 中指定 inets 作为依赖项(当您 $ rebar3 编译)。您仍然需要在文件中的 application 中指定 inets 作为依赖项:

src/your_app_name.app.src

但是 rebar3 本身使用 inets (下载您的依赖项),因此即使您没有在应用程序中将 inets 指定为依赖项,inets 仍会在您的应用之前启动。您可以通过不在应用程序中指定 inets 作为依赖项来测试这一点,然后执行以下操作:

$ rebar3 shell
...
...
1> application:start(inets)
{error,{already_started,inets}}

但是,不要依赖它并指定 inets > 作为应用程序中的依赖项。

In 2019, we use rebar3 to create an application and manage its dependencies. For dependencies that need to be downloaded, you add them to rebar.config, and rebar3 will download the dependencies. For example, if you add hackney (an http client) to rebar.config:

{erl_opts, [debug_info]}.
{deps, [
  {hackney, ".*", {git, "git://github.com/benoitc/hackney.git", {branch, "master"}}}
]}.

{shell, [
  % {config, "config/sys.config"},
    {apps, [http_client]}
]}.

Then do:

../your_app_name$ rebar3 compile

rebar3 will download hackney and compile all the files in the application.

To make sure that all your dependencies get started before your app, you add the names of the dependencies to:

src/your_app_name.app.src

For instance,

{application, http_client,
 [{description, "An OTP application"},
  {vsn, "0.1.0"},
  {registered, []},
  {mod, {http_client_app, []}},
  {applications,
   [kernel,
    stdlib,
    hackney    %%%<=========HERE
   ]},
  {env,[]},
  {modules, []},

  {licenses, ["Apache 2.0"]},
  {links, []}
 ]}.

The actual .app file gets created here:

_build/default/lib/your_app_name/ebin/your_app_name.app

To start your app in the shell along with all its dependencies, you do:

../your_app_name$ rebar3 shell 

The inets application comes with erlang, so it doesn't need to be downloaded, so you don't specify inets as a dependency in rebar.config (you'll get an error when you $ rebar3 compile). You still need to specify inets as a dependency in your application in the file:

src/your_app_name.app.src

But rebar3 itself uses inets (to download your dependencies), so even if you didn't specify inets as a dependency in your application, inets would still get started before your app. You can test that by not specifying inets as a dependency in your application, then doing:

$ rebar3 shell
...
...
1> application:start(inets)
{error,{already_started,inets}}

But, don't rely on that and DO specify inets as a dependency in your application.

蓝海 2024-08-20 11:46:00

如果您的代码打包为应用程序,请列出inets在应用程序资源文件中:

% Filename: ebin/flamingo.app
{application, flamingo,
  [{vsn, "1.0.0"},
   {modules, [flamingo_app,
              flamingo_sup,
              flamingo]},
   {applications, [kernel,
                   stdlib,
                   inets]},
   {mod, {flamingo_app, []}}
  ]}.

然后您可以使用 启动应用程序应用程序:ensure_all_started(flamingo)。这可确保 inets 自动为您启动(即无需显式调用 inets:start())。

例如(假设 *.app 文件和 *.beam 文件位于 ebin/ 中):

$ erl -pa ebin/
Eshell V9.2  (abort with ^G)
1> application:ensure_all_started(flamingo).
{ok,[inets,flamingo]}

If your code is packaged as an application, list inets in the application resource file:

% Filename: ebin/flamingo.app
{application, flamingo,
  [{vsn, "1.0.0"},
   {modules, [flamingo_app,
              flamingo_sup,
              flamingo]},
   {applications, [kernel,
                   stdlib,
                   inets]},
   {mod, {flamingo_app, []}}
  ]}.

Then you can start the application using application:ensure_all_started(flamingo). This ensures that inets is started automatically for you (i.e. there is no need to explicitly call inets:start()).

For example (assuming the *.app file and *.beam files and are in ebin/):

$ erl -pa ebin/
Eshell V9.2  (abort with ^G)
1> application:ensure_all_started(flamingo).
{ok,[inets,flamingo]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文