[erlang-questions] Calling multiple processes simultaneously

Edmond Begumisa <>
Mon May 23 22:50:54 CEST 2011


Another option...

call_gen_servers(Pids) ->
     Ref = erlang:make_ref(),
     Caller = self(),
     lists:foreach(fun(P) ->
                     spawn_link(fun() ->
                                     {reply, ok, _} = gen_server:call(foo,  
bar),
                                     Caller ! {self(), Ref}
                                end).
                   end, Pids),
     ok = wait_gen_servers(length(Pids), Ref).

wait_gen_servers(0, _) ->
     ok;
wait_gen_servers(N, Ref) ->
     receive
         {_, Ref} ->
             wait_gen_servers(N-1, Ref)
     end.

You could also add the timeout.

- Edmond -

On Tue, 24 May 2011 02:01:23 +1000, Martin Dimitrov  
<> wrote:

> Hi, please, advise on this one,
>
> We have several hundred processes (gen_servers) . At one point the
> program has to send them a message that needs to be handled before the
> calling process can continue executing. This naturally led us to use
> gen_server:call in loop over the processes.
>
> I started to wonder if there is a more efficient way of doing it since
> the calling process doesn’t care about the return value – just needs to
> be sure that all processes had handled the message. Can we somehow call
> the processes simultaneously? Something similar to gen_server:multi_call
> but for one node with different processes?
>
> Regards,
>
> Martin
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions


-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/



More information about the erlang-questions mailing list