[erlang-questions] Calling multiple processes simultaneously

Jachym Holecek freza@REDACTED
Mon May 23 18:26:36 CEST 2011


# Martin Dimitrov 2011-05-23:
> 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?

You can steal the logic from gen_server:call/N and adapt it for your
purpose -- basically: setup monitors on all servers, send $gen_call
tagged request to all servers, run a loop receiving responses and
DOWN messages, accumulate successes/failures and return both lists
when done. Also demonitor all non-DOWN servers afterwards.

That's just a sketch off the top of my head -- we have routines like
that for multi-calls towards gen_servers and gen_fsms, IIRC they're
all pretty simple (BTW the only difference between various flavours
is the request tag -- all of the logic is the same), so you shouldn't
have much trouble writing your own.

BR,
	-- Jachym



More information about the erlang-questions mailing list