[erlang-questions] spawn_link
Oscar Hellström
oscar@REDACTED
Mon Jan 5 17:36:52 CET 2009
Hi,
Another side note embedded.
David Budworth wrote:
> quick side note. while John's answer is correct. I think that
> Thilani is trying to spawn a short lived process that creates a socket
> connection.
>
> problem with that is that when the call (connect_to_server) returns,
> the process will exit and the socket will close since the socket lives
> and dies with the process calling gen_tcp:connect(...)
>
> you could hand control over to another process to keep this from
> happening, but this code is clearly not doing so.
> the function is:
> gen_tcp:controlling_process(Socket, NewSocketOwnerPid)
>
> but from a client perspective, this kind of defeats the purpose.
>
> what you really want to do is have the gen server initiate the
> connection itself (from the init method most likely).
Be careful with doing too much work (or at least calling blocking
functions) in the init function of any worker since the supervisor is
blocked until the child returns. If the supervisor is starting a lot of
dynamic children this will become a bottleneck. Connecting a TCP socket
can typically take a long time and will then stop any other child from
being started until the connection has been made.
> then it will own the process. and if it fails to connect, the gen
> server will bomb out letting you know it failed.
>
> if you are trying to make the gen server "generic" and not understand
> how the socket is created, you could always pass in a fun to
> gen_server:start(M,[fun() -> gen_tcp:connect(...)])
>
> then in your gen_server:
> init([ConFun]) ->
> {ok,Socket} = ConFun().
>
> benefits are:
> gen_server doesn't know how the socket is created, but it owns the
> socket itself. so as long as the gen_server is alive, the socket
> lives with it (unless closed by the other end).
>
> Hopefully that was helpful,
>
> -David
>
>
> On Mon, Dec 29, 2008 at 10:39 AM, John Hughes <john.hughes@REDACTED
> <mailto:john.hughes@REDACTED>> wrote:
>
> Hi Thilani,
>
> As Chandru said, you just send the result back to the parent
> process as a
> message. Here's some code:
>
> spawn_call(M,F,As) ->
> Parent = self(),
> Child = spawn_link(fun() -> Parent ! {self(),apply(M,F,As)} end),
> receive {Child,X} -> X end.
>
> Note the receive matches on the child's pid, so can't be confused
> by any
> other messages in the parent's mailbox. Sample call:
>
> 76> foo:spawn_call(lists,reverse,[[1,2,3]]).
> [3,2,1]
>
> John
>
> > From: "Thilani Abeysinghe" <a.thilani@REDACTED
> <mailto:a.thilani@REDACTED>>
> > Subject: [erlang-questions] spawn_link
> > To: erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
> > Message-ID:
> > <d8e8f490812290303y3b89841eu645927839fbe2578@REDACTED
> <mailto:d8e8f490812290303y3b89841eu645927839fbe2578@REDACTED>>
> > Content-Type: text/plain; charset="iso-8859-1"
> >
> > I have a problem of how to get the return value of a function when
> >
> > spawn_link(Fun) ->pid() is used.
> >
> > Im going to use above function in following manner.
> >
> > init([]) ->
> >
> > ChildPid = erlang:spawn_link(?MODULE, connect_to_server,
> [Ip,Port, ConId,
> > self(),State]).
> >
> > connect_to_server(Ip, Port, ConId, ParentPid,State) ->
> > case Result= gen_tcp:connect(Ip, Port,
> > [binary, {packet, 0}]) of
> > {ok, Socket} ->
> > io:format("Socket Received:~w~n",[Socket]),
> > ok =(catch gen_server:call(g_srv, {add_conn, self(), ConId,
> > Socket})),
> > NewState = State#state{socket= Socket};
> > {error, Reason} ->
> > io:format("connection error host Ip:~w Port: ~w ~w ~n",
> > [Ip,Port,Reason]) ,
> > connect_to_server(Ip, Port, ConId, ParentPid,State)
> > end.
> >
> > I want to get the return value of connect_to_server function.
> How can I do
> > that.
> >
> > Regards
> >
> > Thilani
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
Best regards
--
Oscar Hellström, oscar@REDACTED
Office: +44 20 7655 0337
Mobile: +44 798 45 44 773
Erlang Training and Consulting
http://www.erlang-consulting.com/
More information about the erlang-questions
mailing list