[erlang-questions] Performing gen_server:call to multiple processes in parallel

Jachym Holecek <>
Sat Mar 31 14:45:45 CEST 2012


# Martin Dimitrov 2012-03-31:
> What I am trying to achieve is to call several gen_servers in parallel.
> First, I thought I could use gen_server:multi_call/3 but reading the
> documentation (if I understood it correctly) I realized the gen_servers
> need to be on different nodes: one registered gen_server per node.
> 
> Any ideas on how to achieve calling multiple processes on the same node
> in parallel?

Basically steal the body of gen:do_call/4 and adapt a bit. It's easy:
for each destination process create a monitor and reuse the Mref as
transaction reference, then send out request; when done schedule
timeout and go to a loop receiving responses -- you'll probably
want to accumulate one list with successes and other with failures.

In the loop that accumulates results for you be very careful not to
accept message that's not related to your multi_call -- the easiest
way is to loop over generated Mrefs in sequence and block receive
for respective response (or overall timeout).

The Label value in gen:do_call/4 would be one of:

	'$gen_call'
	'$gen_sync_event'
	'$gen_sync_all_state_event'

You want gen_server today, but will want gen_fsm calls tomorrow :-).

HTH,
	-- Jachym



More information about the erlang-questions mailing list