[erlang-questions] question re parallel map code on pg 366 of "Programming Erlang"

David Cabana drcabana@REDACTED
Thu Jan 22 04:51:46 CET 2009


I have a question re Joe's parallel map code on page 366 of Programming Erlang.
Here's the code, modulo a couple of variable name changes.

pmap(F, Arglist) ->
    S = self(),
    TaskID = make_ref(),
    Workers = lists:map( fun(X) ->
                                 spawn( fun() -> do_F(S, TaskID, F, X )  end)
                         end, Arglist),
    gather(Workers, TaskID).

do_F(Caller, TaskID, F, X) ->
    Caller ! {self(), TaskID, catch(F(X))}.

gather([W|R], TaskID) ->
    receive
        {W, TaskID, Val}->
            [Val | gather(R, TaskID)]
    end;

gather([], _) ->
    [].

My question concerns the use of catch inside the do_F function. Here's what Joe
says on page 367:

"In pmap we use catch(F(H)) when we map the function over the list. In
map we just
use F(H). This is because we want to make sure pmap terminates
correctly in the case
where the computation of F(H) raises an exception."

I don't get it.

If the computation of F(H) raises an exception, presumably it would do
so whether F was called by
map or by pmap. Why catch the exception in the one case but not the other?
Why is it important that pmap terminate normally, but not so that map
terminate normally?

Thanks,
drc



More information about the erlang-questions mailing list