How to use rpc:pmap correctly?
Question
I've found in module rpc
function pmap. And I've stuck when tried to use it.
The first question - does it requires connection with other nodes, or it would performs as lists:map
if there is no connection to other nodes?
I've tried to execute pmap
without connection to other nodes, but got exception:
7> rpc:pmap( { erlang, '+' }, [], [[1,1],[2,3]] ).
** exception exit: badrpc
in function rpc:check/2 (rpc.erl, line 745)
After that - I've launched another local node and connected it with current. But still get the same error.
Please provide me how to use rpc:pmap
correctly.
Thanks
P.S. Following code works expectedly (returns result 3):
70> erlang:apply( erlang, '+', [1,2] ).
3
71> erlang:'+'(1,2).
3
La solution
The rpc:map({Module, Function},ExtraArgs,List1) evaluates apply(Module, Function, [Elem|ExtraArgs]), for every element Elem in List1, in parallel.
So the right syntax using erlang:'+' is:
1> rpc:pmap( { erlang, '+' }, [2], [1,2,3] ).
[3,4,5]
2>
which evaluates [1+2,2+2,3+2]
the syntax rpc:pmap( { lists, sum }, [], [[1,2],[4,5]] ) will evaluate [1+2,4+5] and return [3,9]
Pascal