programing

Elixir 애플리케이션을 실행하는 방법은 무엇입니까?

nasanasas 2020. 10. 17. 10:35
반응형

Elixir 애플리케이션을 실행하는 방법은 무엇입니까?


Elixir 애플리케이션을 실행하는 올바른 방법은 무엇입니까?

다음과 같이 간단한 프로젝트를 만들고 있습니다.

mix new app

그 후에 할 수 있습니다.

mix run

기본적으로 내 앱을 한 번 컴파일합니다. 그래서 내가 추가 할 때 :

IO.puts "running"

에서 lib/app.ex내가 보는 "running"첫 번째 시간, 각각의 연속은 run일부 변경이없는 한 아무 것도하지 않습니다. 생성 된 다음으로 무엇을 할 수 app.app있습니까?

물론 내가 할 수 있다는 것을 압니다.

escript: [main_module: App]

에서 mix.exs제공 def main(args):한 다음 :

mix escript.build
./app

하지만 제 생각에는 좀 번거 롭습니다.

다음과 같은 것도 있습니다.

elixir lib/app.exs

그러나 그것은 mix.exs분명히 계산되지 않으며 app.


mix run앱을 실행합니다. 단순히 IO.puts "something"파일에 넣으면 해당 줄은 컴파일 타임에만 평가되며 런타임에는 아무 작업도 수행하지 않습니다. 앱을 시작할 때 무언가를 시작하려면 mix.exs.

일반적으로 Application시작될 최상위 레벨 원합니다 . 이를 달성하려면 다음 mod옵션을 추가 하십시오 mix.exs.

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

그런 다음 해당 모듈에서 애플리케이션 시작시 호출되는 콜백을 구현해야합니다.

defmodule NewMix do
  use Application

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

start콜백 실제로 설치 최상위 프로세스 또는 감독 트리 루트를하지만,이 경우에 당신은 이미 그것을 사용 때마다 호출되는 것을 볼 수 있어야 mix run오류 다음에 있지만,.

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

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.

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.


You can run tasks by importing Mix.Task into your module instead of mix run.

I think this is what you're looking for.

On top of that, instead of mix <task.run>, you can simply run mix to run the default task. Simply add default_task: "bot.run" into the list of def project do [..] end in mix.exs. Refer here.

참고URL : https://stackoverflow.com/questions/30687781/how-to-run-elixir-application

반응형