`
yjl49
  • 浏览: 109763 次
社区版块
存档分类
最新评论

Erlang supervisor 中的simple_one_for_one

阅读更多

        Erlang 中的supervisor子进程的启动策略定义除了one_for_one、one_for_all、rest_for_one还有一种比较常用的simple_one_for_one。

        这种策略与one_for_one比较相似,但在supervisor:init中定义的子进程只能有一个,以后启动的子进程都是以这个为模板产生,且在supervisor 启动时不会主动启动任何子进程,需要自行使用supervisor:start_child 来启动。

        这么做的好处是明显的:例如一个接收客户端连接的socket supervisor进程在刚启动的时候是没有客户端连接上来的,后面也无法确定会有多少个连接上来。所以使用这种动态启动子进程的方式是最合适的。

 

init([Module]) ->
    {ok,
     {_SupFlags = {simple_one_for_one, ?MAX_RESTART, ?MAX_TIME},
      [
       %% TCP Client
       {undefined,
        {Module, start_link, []},
        temporary,
        2000,
        worker,
        []
       }
      ]
     }
    }.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics