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

Gleb Peregud <>
Mon Oct 15 21:36:55 CEST 2012

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

Gleb Peregud

More information about the erlang-questions mailing list