You've just invented pmap :-) - this way of doing it obscures the logic of what's going on:<br><br> pmap(fun() -> gen_server:call(...) end, Pids)<br><br>All the round-trips to the server are done "almost in parallel" (assuming spawn in the initial loop<br>
is faster than the round-trip time to call a server) the execution time for this should be O(T) where<br>T is the longest time for any server to reply.<br><br>pmap/2 is on page 366 of my Erlang book in the module lib_misc.erl<br>
<br><hint>I put all small functions that are generally useful, and do not belong to any particular module<br>in lib_misc.erl</hint><br><br><br>pmap(F, L) -> <br> S = self(),<br> %% make_ref() returns a unique reference<br>
%% we'll match on this later<br> Ref = erlang:make_ref(), <br> Pids = map(fun(I) -> <br> spawn(fun() -> do_f(S, Ref, F, I) end)<br> end, L),<br> %% gather the results<br>
gather(Pids, Ref).<br><br>do_f(Parent, Ref, F, I) -> <br> Parent ! {self(), Ref, (catch F(I))}.<br><br>gather([Pid|T], Ref) -><br> receive<br> {Pid, Ref, Ret} -> [Ret|gather(T, Ref)]<br>
end;<br>gather([], _) -><br> [].<br><br>/Joe<br><br><br><div class="gmail_quote">On Mon, May 23, 2011 at 10:50 PM, Edmond Begumisa <span dir="ltr"><<a href="mailto:ebegumisa@hysteria-tech.com">ebegumisa@hysteria-tech.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Another option...<br>
<br>
call_gen_servers(Pids) -><br>
Ref = erlang:make_ref(),<br>
Caller = self(),<br>
lists:foreach(fun(P) -><br>
spawn_link(fun() -><br>
{reply, ok, _} = gen_server:call(foo, bar),<br>
Caller ! {self(), Ref}<br>
end).<br>
end, Pids),<br>
ok = wait_gen_servers(length(Pids), Ref).<br>
<br>
wait_gen_servers(0, _) -><br>
ok;<br>
wait_gen_servers(N, Ref) -><br>
receive<br>
{_, Ref} -><br>
wait_gen_servers(N-1, Ref)<br>
end.<br>
<br>
You could also add the timeout.<br>
<br>
- Edmond -<div><div></div><div class="h5"><br>
<br>
On Tue, 24 May 2011 02:01:23 +1000, Martin Dimitrov <<a href="mailto:mrtndimitrov@gmail.com" target="_blank">mrtndimitrov@gmail.com</a>> wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hi, please, advise on this one,<br>
<br>
We have several hundred processes (gen_servers) . At one point the<br>
program has to send them a message that needs to be handled before the<br>
calling process can continue executing. This naturally led us to use<br>
gen_server:call in loop over the processes.<br>
<br>
I started to wonder if there is a more efficient way of doing it since<br>
the calling process doesn’t care about the return value – just needs to<br>
be sure that all processes had handled the message. Can we somehow call<br>
the processes simultaneously? Something similar to gen_server:multi_call<br>
but for one node with different processes?<br>
<br>
Regards,<br>
<br>
Martin<br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote>
<br>
<br></div></div><font color="#888888">
-- <br>
Using Opera's revolutionary e-mail client: <a href="http://www.opera.com/mail/" target="_blank">http://www.opera.com/mail/</a></font><div><div></div><div class="h5"><br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</div></div></blockquote></div><br>