[erlang-questions] Sending messages to multiple processes?
David Mercer
dmercer@REDACTED
Fri May 1 16:26:15 CEST 2009
On Thursday, April 30, 2009, Kevin wrote:
> Is there a neato idiom for sending a single message to multiple
> processes, up to say 200, or do I just need to loop it?
[ PID ! Message || PID <- PIDList]>
> I also need to know they finished processing the message. Is this the
> wrong road to go down?
>
> Basically I'm starting 200 processes, reading in a single file, and each
> process is fed the same line of the file one by one until the end, but
> they all handle it differently, and when they have finished all the
> lines, they spit out a modified file, and return a value to indicate
> they are finished.
By "return a value" do you mean that they send a message back? If, so send
the sender's PID along with a reference, and have them send back the value
with the reference to the sent PID.
[ receive {Ref, Val} -> {PID, Val} end || {PID, Ref} <- [ begin Ref =
make_ref(), PID ! {self(), Ref, Message}, {PID, Ref} end || PID <- PIDList]
]
You can break this up into the send and receive parts to make it more
readable, if you like:
RefList = [ begin Ref = make_ref(), PID ! {self(), Ref, Message}, {PID, Ref}
end || PID <- PIDList],
ValList = [ receive {Ref, Val} -> {PID, Val} end || {PID, Ref} <- RefList ]
Cheers,
David Mercer
More information about the erlang-questions
mailing list