[erlang-questions] spawn_link

David Budworth dbudworth@REDACTED
Thu Jan 1 04:27:46 CET 2009


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).

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> 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>
> > Subject: [erlang-questions] spawn_link
> > To: erlang-questions@REDACTED
> > Message-ID:
> > <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
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081231/95a96a8e/attachment.htm>


More information about the erlang-questions mailing list