gen_server and priority queues

Andreas Hillqvist <>
Tue Oct 25 11:21:09 CEST 2005


Why not write:

handle(State) ->
    handle([high, medium, low], State).
    
handle([], State) ->
    receive {_Priority, Msg} ->
        do(Msg, State)
    end;
handle([Priority | Tail], State) ->
    receive {Priority, Msg} ->
            do(Msg, State)
    after 0 ->
            handle(Tail, State)
    end.

Simplifies management of priorities.


Richard Cameron skrev:

>
> I want to implement a server which processes messages in order of  
> priority. I know I can do this in pure Erlang as:
>
> handle(high, State) ->
>     receive {high,Msg} ->
>             do(Msg,State)
>     after 0 ->
>             handle(medium, State)
>     end;
> handle(medium, State) ->
>     receive {medium,Msg} ->
>             do(Msg, State)
>     after 0 ->
>             handle(low, State)
>     end;
> handle(low, State) ->
>     receive {_,Msg} ->
>             do(Msg, State)
>     end.
>
> ... but what I really want is to be able to do this within OTP's  
> gen_server behaviour. Is there a nice way to do this, or do I just  
> need to roll my own server and then attach it to my supervision tree  
> with a bridge?
>
> Richard.
>

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: priorityqueues.erl
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20051025/53abece9/attachment.ksh>


More information about the erlang-questions mailing list