Erlang - Starting a child from the supervisor module

老子叫甜甜 提交于 2019-12-05 17:17:55

Read OTP Doc: in case of simple one for one strategy, in start_child function you can pass arguments as a list for the child start_link function of the child.

When you use simple_one_for_one all the children will use the same ChildSpec, the one given in the init/1 callback and this initialisation can only return one ChildSpec. Also in this case the second argument to supervisor:start_child/2 must be a list and NOT a ChildSpec. This list is a list of extra arguments which is appended to the argument list given in the default ChildSpec and it is this combined argument list which is used when calling the child's start function. This is how the simple_one_for_one children all can use the same ChildSpec and still can get in specific arguments to them.

In your case as there was an empty list in the ChildSpec and you called start_child/2 with an empty list the total number of arguments to the start function was 0. Which matched how your start_link/0 function is defined.

An alternative is in your case to use one_for_one and start each child with its own ChildSpec. More complex but also more versatile.

Unfortunately this dual use of supervisor:start_child/2 has made it inconsistent in its arguments.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!