[erlang-questions] Need some advice on implementing a Queue Server

Gilberio Carmenates Garcia co7eb@REDACTED
Fri Jul 31 20:08:11 CEST 2015


Hi, for that case you could also have a dictionary of queues then name them as  key

Queues = dict:new()

dict:store(<<"Q_1">>, queue:new(), Queues),

 

later you just do:

dict:fetch(<<"Q_1">>, Queues)

 

you can also store the Queues Dictionary in the  gen_server state and retrieve the queue you

want with a simple gen_server:call/2.

 

get_queue(Name)->

    gen_server:call(?MODULE, {get_queue, Name}).

 

handle_call({get_queue, Name} _From, State)->

    %% you can have a well defined data structure in State if you define it as a record you want.

    Queues = State#state.queues,

    R  = dict:fetch(Name, Queues),  %% this throws an error if no key was found so you can use 

                                                       %% dict:find/2  or handle the error.

    {reply, R, State}.

 

Again: apologize for any typo.

 

Best Regards,

Ivan.

 

De: avinash D'silva [mailto:evnix.com@REDACTED] 
Enviado el: viernes, 31 de julio de 2015 0:51
Para: Gilberio Carmenates Garcia
CC: Wes James; erlang-questions@REDACTED
Asunto: Re: [erlang-questions] Need some advice on implementing a Queue Server

 

Thank you both.

 

Gilberio, your example taught me something new today :)

 

BTW, if I want a named queue, should I use   pg2:create(<<"Q_1">>). or is there a better way?

 

On Fri, Jul 31, 2015 at 12:55 AM, Gilberio Carmenates Garcia <co7eb@REDACTED> wrote:

Regards, James, I think using queue module from otp and gen_server behavior, maintaining the Queue in the state, would help you to implement something like that. queue module have many interesting APIs that will do the job for you very faster. Just remember not to use queue:len/1 or any other O(n) order function  if you don´t  need it, for len you can just keep it in track in the gen_server state along with  the queue. NOTE: some of the queue APIs, throws exceptions, use the simple API if you don want to handle exceptions.

 

Ie:

Init([])->

      {ok, {queue:new(), 0}}.

 

Handle_call({push, Item}, _From, {MyQueue, Len})

    {reply, ok, {queue:in(Item, MyQueue), Len + 1}}

 

Handle_call(pop, _From, {MyQueue, Len})

    case queue:out(MyQueue) of

        {{value, Item}, Q} ->

           {reply, Item, {Q, Len - 1}}

        {empty, Q} ->

           {reply, no_item, {Q, Len}}

 

If you want to retrieve some specific item you can always use queue:filter/2 function instead.

Something like this:

pop_item(Item, Queue)->

    queue:filter(fun(I)-> I =/= Item end, Queue).

 

but that is function of order O(n) so…

 

NOTE: I just wrote  the example without testing it, so I apologize for any mistake. 

 

Best regards,

Ivan.

 

 

De: erlang-questions-bounces@REDACTED [mailto:erlang-questions-bounces@REDACTED] En nombre de Wes James
Enviado el: jueves, 30 de julio de 2015 15:02
Para: avinash D'silva
CC: erlang-questions@REDACTED
Asunto: Re: [erlang-questions] Need some advice on implementing a Queue Server

 



On Thu, Jul 30, 2015 at 12:22 PM, avinash D'silva <evnix.com@REDACTED> wrote:
>
> Hi,
>
> I am trying to learn Erlang by implementing a small Queueing Server.
>
> What have I done so far?
> I have created a basic websocket server using cowboy.
>
> I am trying to implement a Queue which all processes can access.
>
> I have used lists which can act as queues.
>
> The basic insert function looks like this:
> Queue(Q) ->
>         receive 
>                Msg ->
>                     Queue(lists:append(Q,[Msg]))
>         end.       
>
> To create a Queue called "Q_1"
>
> >  pg2:create(<<"Q_1">>).
> >  P = spawn(test,Queue,[[1]]). % spawn queue and insert 1.
> >  pg2:join(<<"Q_1">>,P).
> >  P ! 2.
> >  P ! 3. % add some elements to queue.
>
> to retrieve the elements from the named Queue "Q_1":
> 1. I use [Px|_]=pg2:get_members()
> 2. then I would do something like: Px ! POP_ELEMENT
>
>
> Is there a better or more scalable way of doing this?
>
> Will this work when a lot of clients are connected( around 60), having a insert rate of 100k elements per hour?
>
> Any help is appreciated :)
>

I did a search on yahoo for "erlang queue example" and this was the first hit:

http://www.erlang.org/doc/man/queue.html

There were several other examples that might help.

-wes


 





 

-- 

Powered By codologic <http://codologic.com> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150731/3be0c6a7/attachment.htm>


More information about the erlang-questions mailing list