LoginSignup
3
1

More than 5 years have passed since last update.

存在すら無視された gen_server:start_link の返り値

Last updated at Posted at 2017-02-01

最初に断わっておくが、無視し続けても何も困らない可能性があるので、このページを見ても、内容は直ぐに忘れてもらっても構わないと思う。

gen_server:start_link/3, 4 の返り値に ignore がある。
これはどういう時に返ってくるかというと Module:init/1ignore を返した時である。

If Module:init/1 returns {stop,Reason} or ignore, the process is terminated and the function returns {error,Reason} or ignore, respectively.

Module:init/1{stop, Reason} または ignore を返すと、プロセスは停止し関数はそれぞれ {error, Reason}ignore を返す。

さて、この ignore 、何に使うかというと supervisor のページを見れば理由がわかる。

  • If the child process start function returns ignore, the child specification is added to the supervisor (unless the supervisor is a simple_one_for_one supervisor, see below), the pid is set to undefined, and the function returns {ok,undefined}.

  • 子プロセスの start 関数が ignore を返した場合、子の仕様は supervisor に加えられるが pid は undefined に設定され、( start_child )関数は {ok, undefined} を返す。

つまり、プロセスは起動させないが ChildSpec を登録するというようなことができる。

この影響で supervisor:child() の型には pid() だけでなく undefined が含まれるので注意しなければならない。

ちなみに、 simple_one_for_one の時は子の仕様は追加されない。

For a simple_one_for_one supervisor, when a child process start function returns ignore, the functions returns {ok,undefined} and no child is added to the supervisor.

simple_one_for_one の supervisor においては、子プロセスの start 関数が ignore を返した時、( start_child )関数は {ok, undefined} を返し、子は supervisor に追加しない。

もちろん、 supervisor:start_link/2, 3ignore を返せる。

ただ、めったに使うことがないと思われるので、皆さん ignore しておいてもいいのではないでしょうか。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1