【问题标题】:Using elixir applications for programs that run only once对只运行一次的程序使用 elixir 应用程序
【发布时间】:2017-02-11 05:08:27
【问题描述】:

启动应用程序时,返回值必须是 pid 或错误。

是否可以将应用程序用于只运行一次的程序。类似的东西。

defmodule MyApp do
  use Application

  def start(_type, _args) do
    # Do stuff
    {:done, :normal}
  end
end

【问题讨论】:

  • 应用程序适用于需要健壮的长时间运行的程序...如果这是一次性的,例如控制台脚本,为什么不将其建模为脚本( .exs?)。如果它是稳健的,为什么应用程序模型会给您带来麻烦?
  • 为什么需要一个 OTP 应用程序?
  • 所以答案是不使用应用程序并制作 escript?我不确定是什么构成了正确的混音项目。也许问题是对于单个运行程序的混合项目是什么样的。
  • 您可能不需要更改 mix 设置项目的方式。如果您需要停止您的应用程序,为什么不在完成后直接 Application.stop 它?

标签: elixir erlang-otp elixir-mix


【解决方案1】:

你把事情复杂化了@Peter。你想要的是一个 Elixir 脚本(exs 文件)。从这个例子开始:

defmodule MyApp do
  def my_test_func do
    IO.puts "Hello world!"
  end
end

MyApp.my_test_func

将该代码另存为test1.exs。然后你可以通过elixir test1.exs从命令提示符运行它我的意思是你不需要gen_server来创建一个简单的脚本。

您可以在the System docs 中找到有关与操作系统交互的更多信息,您可以在此处查看有关 Elixir 脚本主题的更多信息:http://elixir-lang.org/getting-started/introduction.html#running-scripts

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多