Back

elixir - 23 supervisor 和 application

发布时间: 2019-03-08 01:54:00

参考: https://elixir-lang.org/getting-started/mix-otp/supervisor-and-application.html

创建一个新文件:lib/kv/supervisor.ex

defmodule KV.Supervisor do
  use Supervisor

  def start_link opts do
    Supervisor.start_link __MODULE__, :ok, opts
  end 

  def init :ok do
    children = [
      KV.Registry
    ]   

    # one_for_one 表示,某个子进程死掉后, 再启动一个新进程
    Supervisor.init(children, strategy: :one_for_one)
  end 
end

使用 use Gen, use Agent 或者 use Supervisor , 都会自动生成一个 child_spec/1 方法。

Back