[erlang-questions] value of spawned function
Jason Ganetsky
jason.ganetsky@REDACTED
Fri Sep 19 17:49:09 CEST 2008
Try something like this...
-module(forkjoin).
-compile(export_all).
fork(FunList) when is_list(FunList) ->
process_flag(trap_exit, true),
ParentPid = self(),
[begin
WrappedFun =
fun () ->
ParentPid ! {'RESULT', self(), Fun()}
end,
spawn_link(WrappedFun)
end ||
Fun <- FunList].
join(PidList) when is_list(PidList) ->
[receive
{'RESULT', Pid, Result} ->
{ok, Result};
{'EXIT', Pid, Reason} ->
{error, Reason}
end ||
Pid <- PidList].
On Fri, Sep 19, 2008 at 11:43 AM, Gleb Peregud <gleber.p@REDACTED> wrote:
> You may try to use erlang:exit/1 to make 'EXIT'/'DOWN' message
> "return" something instead of 'normal'.
>
> Take a look at plists: http://code.google.com/p/plists/ - it
> implements quite a lot of parallel list operations, which may be what
> you are looking for :)
>
> On Fri, Sep 19, 2008 at 4:06 PM, Zvi <exta7@REDACTED> wrote:
> >
> > Hi,
> >
> > I trying to implement some Fork-Join constructs in Erlang using links or
> > monitors.
> > I do not understand why when process finished executing function
> normally,
> > it's (function's) return value isn't put into 'EXIT' or 'DOWN' messages?
> > I.e. instead of atom 'normal' return {normal, ResultOfFun} or something
> > similar.
> >
> > {'EXIT',Pid,normal}
> >
> > {'DOWN',Ref,process,Pid,normal}
> >
> >
> > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> > -module(fj).
> > -compile(export_all).
> >
> > -define(N, erlang:system_info(schedulers) ).
> >
> > test_monitor() ->
> > % fork
> > PidRefs = [spawn_monitor(
> > fun() -> io:format("pid: ~w~n",[self()]) end) ||
> > _<-lists:seq(1,?N)],
> > io:format("PidRefs: ~w~n", [PidRefs]),
> > % join
> > [receive {'DOWN',Ref,process,Pid,_}->ok end || {Pid,Ref}<-PidRefs
> ].
> >
> > test_link() ->
> > % fork
> > process_flag(trap_exit, true),
> > Pids = [spawn_link(
> > fun() -> io:format("pid: ~w~n",[self()]) end) ||
> > _<-lists:seq(1,?N)],
> > io:format("Pids: ~w~n", [Pids]),
> > % join
> > [receive {'EXIT',Pid,_}->ok end || Pid<-Pids ].
> >
> > P.S.
> > if there is some better ways to implement fork join please let me know.
> >
> > Zvi
> > --
> > View this message in context:
> http://www.nabble.com/value-of-spawned-function-tp19573059p19573059.html
> > Sent from the Erlang Questions mailing list archive at Nabble.com.
> >
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
>
>
>
> --
> Gleb Peregud
> http://gleber.pl/
>
> Every minute is to be grasped.
> Time waits for nobody.
> -- Inscription on a Zen Gong
> _______________________________________________
> 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/20080919/55c1310d/attachment.htm>
More information about the erlang-questions
mailing list