[erlang-questions] spawn_link

Chandru <>
Mon Dec 29 12:10:23 CET 2008


2008/12/29 Thilani Abeysinghe <>:
> 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.
>

I'm assuming you want to send intermediate results, as
connect_to_server seems to be part of a process which is long lived.
In such cases, send a message to the parent process from the spawned
process.

cheers
Chandru



More information about the erlang-questions mailing list