[erlang-questions] Reinserting a message into a gen_server's mailbox

Gleb Peregud <>
Thu Jul 29 17:55:28 CEST 2010


On Thu, Jul 29, 2010 at 17:15, Juan Jose Comellas <> wrote:
> 1) The "correct" one: keep the list of requests that come in while the data
> is not ready and once I have the data in the helper process, notify the
> gen_server process and do a gen_server:reply/2 with the response for each of
> the pending requests.
AFAIK there is no need to do gen_server:reply/2 from the gen_server
itself - one can send reply from absolutely any process. So just
proc_lib:spawn_link/1 a worker process for long tasks and do
gen_server:reply/2 in it when it finishes the task. In fact there is
even no need to store a list of requests in gen_server.

> 2) The easy (non-kosher) one: while the data is not ready reinsert all the
> requests (that depend on the data) that come into the gen_server process
> back into its own mailbox (maybe with a little delay) and hope that when
> they are processed again the data will be ready.
This is a form of polling. Why do you want one? "Correct" solution is
much-much more elegant and efficient. I can't see a situation when
this solution would be better...

Best,
Gleb Peregud


More information about the erlang-questions mailing list