[erlang-questions] using flush() for batches from mailbox

AD <>
Tue Oct 16 00:04:44 CEST 2012


Why would callers timeout (if using cast), is b/c the flush would prevent
the gen_server from sending a reply ?

On Mon, Oct 15, 2012 at 3:36 PM, Gleb Peregud <> wrote:

> On Mon, Oct 15, 2012 at 7:31 PM, AD <> wrote:
> > Hello,
> >
> >  I have a use case where I would like to flush() the Mailbox in a loop.
>  I
> > have a process that takes about 500ms to execute, and i would like the
> > process to operate on batches of messages from the Mailbox rather than 1
> at
> > a time.  Since the sender can push messages to the mailbox at tens of
> > messages/sec and I would like to preserve order, the most efficient way
> > right now I think of is to take messages off the mailbox in batches.
> >
> > If there is a process sending to a gen_server:cast at , say, 10
> messages/sec
> > , can i use flush to consume all 10 messages from teh mailbox at once?
> > Something like
> >
> > flush() ->
> >         receive
> >                 _ -> flush()
> >         after
> >                 0 -> ok
> >         end.
>
> Of course you can. The code is correct as well. There are two problems
> here:
> 1) It will consume gen_server:calls, hence callers will timeout
> 2) It will consume sys messages, hence e.g. sys:get_status/1 won't work
>
> Cheers,
> Gleb Peregud
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121015/192513a8/attachment.html>


More information about the erlang-questions mailing list