如何运行Elixir应用程序? [英] How to run an Elixir application?

查看:112
本文介绍了如何运行Elixir应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行Elixir应用程序的正确方法是什么?

What is the correct way to run an Elixir application?

我正在通过以下方式创建一个简单的项目

I'm creating a simple project by:

mix new app

然后我可以做:

mix run

基本上可以一次编译我的应用程序.所以当我添加:

which basically compiles my app once. So when I add:

IO.puts "running"

lib/app.ex 中的

我只第一次看到"running" ,除非有一些操作,否则每个连续的 run 都不执行变化.下一步如何使用生成的 app.app ?

in lib/app.ex I see "running" only for the first time, each consecutive run does nothing unless there are some changes. What can I do next with generated app.app?

我当然知道我能做到:

escript: [main_module: App]

mix.exs 中的

中,提供 def main(args):,然后:

mix escript.build
./app

但是我觉得这有点麻烦.

but's it's kinda cumbersome in my opinion.

还有类似的东西:

elixir lib/app.exs

,但显然不算 mix.exs ,这是我 app 中的依赖项所必需的.

but it does not count mix.exs obviously, which is needed for dependencies in my app.

推荐答案

混合运行确实可以运行您的应用.只是当您简单地将 IO.puts某物" 放入文件中时,该行仅在编译时进行评估,而在运行时则不执行任何操作.如果您想在启动应用程序时入门,则需要在 mix.exs 中指定.

mix run does run your app. It's just that when you simply put IO.puts "something" in a file that line is only evaluated in compile-time, it does nothing at runtime. If you want something to get started when you start your app you need to specify that in your mix.exs.

通常,您需要一个入门的顶级 Application .为此,在您的 mix.exs 中添加 mod 选项:

Usually you want a top-level Application that will get started. To achieve that add a mod option to your mix.exs:

def application do
  [
    # this is the name of any module implementing the Application behaviour
    mod: {NewMix, []},
    applications: [:logger]
  ]
end

然后在该模块中,您需要实现一个将在应用程序启动时调用的回调:

And then in that module you need to implement a callback that will be called on application start:

defmodule NewMix do
  use Application

  def start(_type, _args) do
    IO.puts "starting"
    # some more stuff
  end
end

start 回调实际上应该设置您的顶级过程或监督树的根,但是在这种情况下,您已经看到每次使用 mix run 时都会调用它,尽管出现错误.

The start callback should actually setup your top-level process or supervision tree root but in this case you will already see that it is called every time you use mix run, although followed by an error.

def start(_type, _args) do
  IO.puts "starting"
  Task.start(fn -> :timer.sleep(1000); IO.puts("done sleeping") end)
end

在这种情况下,我们在回调中开始一个简单的过程,该过程仅睡眠一秒钟然后输出一些内容-足以满足 start 回调的API,但我们看不到睡着了" .原因是默认情况下,一旦回调执行完毕, mix run 将退出.为了避免这种情况发生,您需要使用 mix run --no-halt -在这种情况下,VM不会停止.

In this case we are starting a simple process in our callback that just sleeps for one second and then outputs something - this is enough to satisfy the API of the start callback but we don't see "done sleeping". The reason for this is that by default mix run will exit once that callback has finished executing. In order for that not to happen you need to use mix run --no-halt - in this case the VM will not be stopped.

启动应用程序的另一种有用的方法是 iex -S mix -行为与 mix run --no-halt 类似,但是还会打开一个 iex shell,您可以在其中与代码和正在运行的应用程序进行交互.

Another useful way of starting your application is iex -S mix - this will behave in a similar way to mix run --no-halt but also open up an iex shell where you can interact with your code and your running application.

这篇关于如何运行Elixir应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆