如何运行Elixir应用程序? [英] How to run Elixir application?
问题描述
运行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
中,我看到正在运行
仅是第一次,除非有一些更改,否则每次连续的 都不会执行任何操作。下一步,如何使用生成的
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
?
当然我知道我可以做到:
Of course I know I can do:
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
开始
回调实际上应该设置您的顶级过程或监督树的根,但是在这种情况下,您已经看到每次使用 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 --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屋!