| Author |
Message |
|
| kornerhill |
Posted: Sat Apr 24, 2010 2:22 pm |
|
|
|
Joined: 24 Apr 2010
Posts: 6
|
I am a newbie to erlang, and I write a script to test supervisor, but it seems there is something wrong with this script. I just cannot figure out why. my script is attached at the end of my post. It compiles successfully, but I run it in the shell and get an exception:
8> superv:start().
** exception exit: shutdown
I thought the child process should run forever, why does it shutdown? Could anyone figure me out?
%% filename : superv.erl
-module(superv).
-compile(export_all).
-behaviour(supervisor).
start() ->
supervisor:start_link(superv, []).
init(Param) ->
{ok,{{one_for_one, 100, 2},[{proc1, {superv, start_calc, []}, permanent,
500, worker, [superv]}]}}.
start_proc() ->
spawn_link(superv, loop, []).
loop() ->
io:format("loop"),
receive X -> X end,
loop(). |
|
|
| Back to top |
|
| zajda |
Posted: Sun Apr 25, 2010 10:42 am |
|
|
|
User
Joined: 22 Aug 2009
Posts: 83
|
First of all, there is a typo in your child_spec, should be start_proc not start_calc:
Code: [{proc1, {superv, start_proc, []}, permanent,
500, worker, [superv]}]
More important is that it will not work anyway, because in start_proc you only start new process end exit with normal reason..
So, it doesnt make any sense to hook up to supervisor process which exits at the beginning. To fix it, copy paste body of loop to start_proc.
Usually it is gen_server, gen_fsm, gen_event or supervisor what is building supervision tree.
Michał Zajda
------------------
Erlang Solutions Ltd |
|
|
| Back to top |
|
| kornerhill |
Posted: Sun Apr 25, 2010 12:02 pm |
|
|
|
Joined: 24 Apr 2010
Posts: 6
|
| hi, zajda, thanks! It works. I thought start_proc/0 was used to create the worker process, but it seems that start_proc/1 itself is the process loop. |
|
|
| Back to top |
|
| zajda |
Posted: Sun Apr 25, 2010 1:11 pm |
|
|
|
User
Joined: 22 Aug 2009
Posts: 83
|
hi, one more thing: try to stick to regular function's names i.e. start_link instead of start.
Also using export_all is not recommended (it makes a mess - no clear api and produces worse code after compilation).
Michał Zajda
------------------
Erlang Solutions Ltd |
|
|
| Back to top |
|
| kornerhill |
Posted: Mon Apr 26, 2010 2:20 am |
|
|
|
Joined: 24 Apr 2010
Posts: 6
|
|
| Back to top |
|
| MathewBracken |
Posted: Wed Jun 02, 2010 6:36 am |
|
|
|
Joined: 24 Feb 2010
Posts: 3
|
| More important is that it will not work anyway, because in start_process you only start new process end exit with normal reason. He is always manage all workers. |
|
|
| Back to top |
|
| zajda |
Posted: Fri Jun 04, 2010 4:43 pm |
|
|
|
User
Joined: 22 Aug 2009
Posts: 83
|
I guess you wanted to quote 1st sentence (mine) and comment it (second sentence).
Yes, supervisor watches all workers = the specific processes which are configured in supervisor init/1. But if those processes spawn some new processes supervisor knows nothing about them, cannot control them any how. |
|
|
| Back to top |
|
|
|