Elixir 的 Supervisor 配置
只有在 Elixir 模块中定义了 child_spec
方法,此模块才能被 Supervisor 监控,可以通过两种方式配置 Supervisor 的参数 children ,一种是内置模块中已经实现了 child_spec 方法,例如 gen_server, agent 等,可以直接:
children = [ # Starts a worker by calling: Fire.Worker.start_link(arg) # {Fire.Worker, arg}, {Fire, []} ]
另外一种是显式配置:
children = [ # Starts a worker by calling: Fire.Worker.start_link(arg) # {Fire.Worker, arg}, # {Fire, []}, %{ id: Fire, start: {Fire, :start_link, []}, restart: :permanent, shutdown: 500 } ]
也可以在模块中实现 child_spec ,这样就可以直接使用上一种配置方式:{Fire, []}
defmodule Fire do def start_link() do Task.start_link(fn -> IO.puts "hello elixir" end) end def child_spec(args) do %{ id: __MODULE__, start: {__MODULE__, :start_link, args} } end end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论