开发者

Supervisors behaviour

开发者 https://www.devze.com 2023-03-02 17:21 出处:网络
when implementing a supervisor.. If, in the supervisor module, I do something like init([_]) -> {ok, {{one_for_one, 5, 60},

when implementing a supervisor.. If, in the supervisor module, I do something like

init([_]) ->

{ok,

{{one_for_one, 5, 60},

[{reverese, {reverse, start_reverse, []}, permanent, brutal_kill, worker,[]}]}}.

and the reverse function is:

开发者_如何学运维

start_reverse() ->

Pid=spawn(?MODULE,reverse,[]).

It will not work since the start_reverse function exits normally in every case. However, when I add a line like this:

start_reverse() ->

Pid=spawn(?MODULE,reverse,[]),

{ok,Pid}.

It works, even when the function exits normally as well. Can someone explain why?


Easily,

The problem is that the supervisor needs a specific calling convention to support the shutdown and initialization of processes. Your code with a low-level spawn ignores that convention. You should either

  • Use a gen_something behaviour, gen_server is most common.
  • Spawn the process using proc_lib
  • Use a supervisor_bridge

Otherwise, your code will not take advantage of OTP at all. And you want it to.


Actually.. It isnt really required for the supervisor child process to be a gen_server. The supervisor documentation specifically mentions that

The start function must create and link to the child process, and should return {ok,Child} or {ok,Child,Info} where Child is the pid of the child process and Info an arbitrary term which is ignored by the supervisor.

which is the reason why when you returned {ok, Pid} it worked..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号