Erlang/OTP Forums

Author Message

<  Open Telecom Platform (OTP)  ~  Spawning a Supervisor

floob
Posted: Fri May 07, 2010 9:25 pm Reply with quote
Joined: 07 May 2010 Posts: 5
If anyone would like some easy points on StackOverflow, I originally posted this question over there (link).

I'm new to erlang, I've been reading through the online documentation, and I'm trying to get a grip on the basics of the OTP framework.

I've found that when testing a gen_server independently, from the command line, I can spawn a process with my gen_server implementation's start_link function and interact with the server just fine.

e.g.

Code:

1> spawn(echo_server, start_link, []).
<0.37.0>
2> echo_server:echo(hi).
hi


This is not the case for my supervisor. If I spawn a process that runs my supervisor's start_link, it appears to start the supervisor and gen_server (SASL turned on, I get a progress report saying they've started), but I cannot interact with my gen_server.

e.g.

Code:

1> spawn(echo_sup, start_link, []).
<0.37.0>
2> echo_server:echo(hi).
** exception exit: {noproc,{gen_server,call,[echo_server,{echo,hi}]}}
     in function  gen_server:call/2


In both implementations (supervisor and gen_server), their respective start_link methods call the appropriate module's start_link function (supervisor and gen_server, respectively) with
Code:
...:start_link({local, ?MODULE}, ?MODULE, [])


Can someone explain the discrepancy?
View user's profile Send private message
zajda
Posted: Sat May 08, 2010 4:17 pm Reply with quote
User Joined: 22 Aug 2009 Posts: 83
please, copy-paste the code of your supervisor.








MichaƂ Zajda
------------------
Erlang Solutions Ltd
View user's profile Send private message
Michal Ptaszek
Posted: Sat May 08, 2010 4:19 pm Reply with quote
User Joined: 01 May 2008 Posts: 35 Location: Krakow
When you are starting supervisor, you run:

Code:

1> spawn(echo_sup, start_link, []).
<0.37.0>


The new process is spawned, (pid <0.37.0>). Within its context the supervisor:start_link is called which creates new supervisor process for you and links it to <0.37.0>. Since execution of echo_sup:start_link/0 reaches end of the function, <0.37.0> terminates and sends an exit signal to the supervisor causing its termination which in turn is propagated down, to your gen_server.

In order to avoid this situation run echo_sup:start_link/0 directly from shell, without spawning new process.

Ideally you would use the application behaviour and start it via: application:start(my_app).
View user's profile Send private message
floob
Posted: Sat May 08, 2010 7:49 pm Reply with quote
Joined: 07 May 2010 Posts: 5
@Michal Ptaszek: That would explain why I can't spawn a supervisor, but that doesn't explain why I can spawn a gen_server in the same way.

Also, if the death of the spawned process <0.37.0> propogated death to my supervisor, wouldn't the SASL application have reported the death of the supervisor and gen_server? It only shows a progress report that they've started.

@zajda

The gen_server code can be found here, if you need

echo_sup.erl:

Code:

-module(echo_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).

start_link() ->
    supervisor:start_link(echo_sup, []).
init(_Args) ->
    {ok,  {{one_for_one, 5, 60},
       [{echo_server, {echo_server, start_link, []},
             permanent, brutal_kill, worker, [echo_server]}]}}.
View user's profile Send private message
nike air max
Posted: Thu Jan 05, 2012 1:54 am Reply with quote
Guest
nike air max in the higher mysteries of the most august religions it has been made the symbol of the divine spotlessness and power. features a black suede upper with premium black leather working the toebox and side panels.
babyjing
Posted: Thu Jan 12, 2012 2:38 am Reply with quote
Guest
Popular nfl jerseys cheap are going to be scarce over the next three months.Then they're going to be more expensive.The most popular teams will experience the worst shortages, and the James Jones Jersey are breaking all sorts of popularity records this year, from on-field performance to stock sales to merchandise sales.To lure the customer towards their service, providers are leaving no stone unturned in order to provide several plans to make Josh Sitton Jersey.Thats why your website's critics are so vital, and ofcourse why companies must be alerted at all times.With the sport's new passing-crazed style of play challenging the traditional notion that Buffalo Sabres jerseys is a fundamental ingredient for postseason success, these Nick Collins Jersey will provide an intriguing look at the way to win in the NFL when the stakes are highest.

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum