<div dir="ltr">Thank you both.<div><br></div><div>Gilberio, your example taught me something new today :)</div><div><br></div><div>BTW, if I want a named queue, should I use <span style="font-size:12.8000001907349px"> </span><span style="color:black;font-size:12px;line-height:1.1em;background-color:rgb(249,249,249)"> pg2:create(<<"Q_1">>). or is there a better way?</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 31, 2015 at 12:55 AM, Gilberio Carmenates Garcia <span dir="ltr"><<a href="mailto:co7eb@frcuba.co.cu" target="_blank">co7eb@frcuba.co.cu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="ES" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">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.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Ie:<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Init([])-><u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> {ok, {queue:new(), 0}}.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Handle_call({push, Item}, _From, {MyQueue, Len})<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> {reply, ok, {queue:in(Item, MyQueue), Len + 1}}<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Handle_call(pop, _From, {MyQueue, Len})<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> case queue:out(MyQueue) of<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> {{value, Item}, Q} -><u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> {reply, Item, {Q, Len - 1}}<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> {empty, Q} -><u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> {reply, no_item, {Q, Len}}<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">If you want to retrieve some specific item you can always use
queue:filter/2 function instead.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Something like this:<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">pop_item(Item, Queue)-><u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> queue:filter(fun(I)-> I =/= Item end, Queue).<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">but that is function of order O(n) so…<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"> <u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">NOTE: I just wrote the example without testing it, so I
apologize for any mistake. <u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Best regards,<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Ivan.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">De:</span></b><span lang="EN-US" style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> <a href="mailto:erlang-questions-bounces@erlang.org" target="_blank">erlang-questions-bounces@erlang.org</a>
[mailto:<a href="mailto:erlang-questions-bounces@erlang.org" target="_blank">erlang-questions-bounces@erlang.org</a>] <b>En nombre de </b>We</span><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">s James<br>
<b>Enviado el:</b> jueves, 30 de julio de 2015 15:02<br>
<b>Para:</b> avinash D'silva<br>
<b>CC:</b> <a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<b>Asunto:</b> Re: [erlang-questions] Need some advice on implementing a Queue
Server<u></u><u></u></span></p>
</div><div><div class="h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
<br>
On Thu, Jul 30, 2015 at 12:22 PM, avinash D'silva <<a href="mailto:evnix.com@gmail.com" target="_blank">evnix.com@gmail.com</a>> wrote:<br>
><br>
> Hi,<br>
><br>
> I am trying to learn Erlang by implementing a small Queueing Server.<br>
><br>
> What have I done so far?<br>
> I have created a basic websocket server using cowboy.<br>
><br>
> I am trying to implement a Queue which all processes can access.<br>
><br>
> I have used lists which can act as queues.<br>
><br>
> The basic insert function looks like this:<br>
> Queue(Q) -><br>
> receive <br>
> Msg -><br>
>
Queue(lists:append(Q,[Msg]))<br>
> end. <br>
><br>
> To create a Queue called "Q_1"<br>
><br>
> > pg2:create(<<"Q_1">>).<br>
> > P = spawn(test,Queue,[[1]]). % spawn queue and insert 1.<br>
> > pg2:join(<<"Q_1">>,P).<br>
> > P ! 2.<br>
> > P ! 3. % add some elements to queue.<br>
><br>
> to retrieve the elements from the named Queue "Q_1":<br>
> 1. I use [Px|_]=pg2:get_members()<br>
> 2. then I would do something like: Px ! POP_ELEMENT<br>
><br>
><br>
> Is there a better or more scalable way of doing this?<br>
><br>
> Will this work when a lot of clients are connected( around 60), having a
insert rate of 100k elements per hour?<br>
><br>
> Any help is appreciated :)<br>
><br>
<br>
I did a search on yahoo for "erlang queue example" and this was the
first hit:<br>
<br>
<a href="http://www.erlang.org/doc/man/queue.html" target="_blank">http://www.erlang.org/doc/man/queue.html</a><u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">There were several other
examples that might help.<u></u><u></u></p>
</div>
<p class="MsoNormal">-wes<u></u><u></u></p>
<div>
<div>
<p class="MsoNormal"><br>
<u></u><u></u></p>
</div>
</div>
</div>
</div></div></div>
</div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">Powered B<span></span><span></span>y <a href="http://codologic.com" target="_blank">codologic</a></div>
</div>