# Asynchronous select from queue

**URL:** <https://www.ruby-forum.com/t/asynchronous-select-from-queue/78900>\
**Category:** Ruby\
**Created:** [December 10, 2006, 9:50am UTC](https://www.ruby-forum.com/t/asynchronous-select-from-queue/78900 "2006-12-10T09:50:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![borisu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@borisu](https://www.ruby-forum.com/u/borisu)\
**Post date:** [December 10, 2006, 9:50am UTC](https://www.ruby-forum.com/t/asynchronous-select-from-queue/78900/1 "2006-12-10T09:50:32Z")

</div>

Please advise on how to implement multiple select from queues in Ruby. I  
have seen only selcet which operates on IO streams. I need to pass  
objects not symbols. I have 100 queues and I must wait when message  
appears on one of them.

---

<div class="post-metadata">

**Author:** ![borisu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@borisu](https://www.ruby-forum.com/u/borisu)\
**Post date:** [December 10, 2006, 10:34am UTC](https://www.ruby-forum.com/t/asynchronous-select-from-queue/78900/2 "2006-12-10T10:34:53Z")

</div>

On Dec 10, 2006, at 24:50 , Boris Mojo-jojo wrote:

> Please advise on how to implement multiple select from queues in  
> Ruby. I  
> have seen only selcet which operates on IO streams. I need to pass  
> objects not symbols. I have 100 queues and I must wait when message  
> appears on one of them.

ri Queue ?

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![borisu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@borisu](https://www.ruby-forum.com/u/borisu)\
**Post date:** [December 10, 2006, 10:40am UTC](https://www.ruby-forum.com/t/asynchronous-select-from-queue/78900/3 "2006-12-10T10:40:55Z")

</div>

Eric H. wrote:

> On Dec 10, 2006, at 24:50 , Boris Mojo-jojo wrote:
> 
> > Please advise on how to implement multiple select from queues in  
> > Ruby. I  
> > have seen only selcet which operates on IO streams. I need to pass  
> > objects not symbols. I have 100 queues and I must wait when message  
> > appears on one of them.
> 
> ri Queue ?
> 
> –  
> Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)
> 
> I LIT YOUR GEM ON FIRE!

Sorry but no info on select in ri. Look for yourslef

C:\ruby\bin\>ri Queue  
----------------------------------------------------------- Class: Queue  
This class provides a way to synchronize communication between  
threads.

```
 Example:

   require 'thread'

   queue = Queue.new

   producer = Thread.new do
     5.times do |i|
       sleep rand(i) # simulate expense
       queue << i
       puts "#{i} produced"
     end
   end

   consumer = Thread.new do
     5.times do |i|
       value = queue.pop
       sleep rand(i/2) # simulate expense
       puts "consumed #{value}"
     end
   end

   consumer.join

```

* * *

## Class methods:

```
 new

```

## Instance methods:

```
 <<, clear, deq, empty?, enq, length, num_waiting, pop, push, shift,
 size

```

---

<div class="post-metadata">

**Author:** ![borisu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@borisu](https://www.ruby-forum.com/u/borisu)\
**Post date:** [December 10, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/asynchronous-select-from-queue/78900/4 "2006-12-10T18:15:30Z")

</div>

Boris Mojo-jojo wrote:

> Sorry but no info on select in ri. Look for yourslef

“select” only works on IO objects - that’s it’s purpose. The reason for  
pointing you to Queue was that you indicated you’re using queues, in  
which case the ri documentation is your best starting point.

#select is in any case the completely wrong place to look unless you’ve  
implemented your queues as pipes or similar.

If you want any more useful help you’ll need to give a more complete  
description of what you are trying to achieve.

Vidar

---

<div class="post-metadata">

**Author:** ![borisu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@borisu](https://www.ruby-forum.com/u/borisu)\
**Post date:** [December 11, 2006, 10:47am UTC](https://www.ruby-forum.com/t/asynchronous-select-from-queue/78900/5 "2006-12-11T10:47:09Z")

</div>

On 10.12.2006 18:12, Vidar H. wrote:

> If you want any more useful help you’ll need to give a more complete  
> description of what you are trying to achieve.

Adding to that, application architectural wise I do not see the benefit  
of having multiple queues if you want to read from all of them and treat  
objects identically anyway. This is a typical scenario where a single  
queue is sufficient and in fact the most efficient design. At the  
moment I cannot think of a reason why multiple queues must be there.  
Boris, can you elaborate?

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![borisu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@borisu](https://www.ruby-forum.com/u/borisu)\
**Post date:** [December 11, 2006, 12:06pm UTC](https://www.ruby-forum.com/t/asynchronous-select-from-queue/78900/6 "2006-12-11T12:06:56Z")

</div>

Robert K. wrote:

> On 10.12.2006 18:12, Vidar H. wrote:
> 
> > If you want any more useful help you’ll need to give a more complete  
> > description of what you are trying to achieve.
> 
> Adding to that, application architectural wise I do not see the benefit  
> of having multiple queues if you want to read from all of them and treat  
> objects identically anyway. This is a typical scenario where a single  
> queue is sufficient and in fact the most efficient design. At the  
> moment I cannot think of a reason why multiple queues must be there.  
> Boris, can you elaborate?
> 
> Kind regards
> 
> robert

Suppose you have 1000 devices which are being sent messages, every  
device has its own queue. devices are independent so the messages to  
them must be processed in parallel. Processing message in one device  
should not stop processing others. However as along as you cannot create  
1000 threads, there is a thread pool where every thread waits for  
message to device X to arrive on one of the 1000 queues and processed it  
in context of device X. If some thread is processing message which was  
sent to device X then the queue X is excluded from thread pool to  
prevent parallel executing of message on the device by two different  
threads.

You don’t want to use one queue, because when two messages are sent to  
one device one after another then all other messages will wait because  
second message will not be processed till first one is processed.

Now once again the question. How do I do select from multiple queues?  
in Win32 there’s WaitForMultipleObjects  
in Java there’s Selector and Pipes  
in Ruby there’s ???

---

<div class="post-metadata">

**Author:** ![borisu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@borisu](https://www.ruby-forum.com/u/borisu)\
**Post date:** [December 11, 2006, 2:07pm UTC](https://www.ruby-forum.com/t/asynchronous-select-from-queue/78900/7 "2006-12-11T14:07:06Z")

</div>

Robert K. wrote:

> On 11.12.2006 12:06, Boris Mojo-jojo wrote:
> 
> > You don’t want to use one queue, because when two messages are sent to  
> > one device one after another then all other messages will wait because  
> > second message will not be processed till first one is processed.
> 
> True, in that case “select” would help. But you can easily write your  
> own processor with thread pool. You can get ideas from Queue internals  
> or [http://wiki.rubygarden.org/Ruby/page/show/MultiThreading](http://wiki.rubygarden.org/Ruby/page/show/MultiThreading)
> 
> Kind regards
> 
> robert

Thanks a lot for the link

---

<div class="post-metadata">

**Author:** ![borisu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@borisu](https://www.ruby-forum.com/u/borisu)\
**Post date:** [December 11, 2006, 1:35pm UTC](https://www.ruby-forum.com/t/asynchronous-select-from-queue/78900/8 "2006-12-11T13:35:26Z")

</div>

On 11.12.2006 12:06, Boris Mojo-jojo wrote:

> You don’t want to use one queue, because when two messages are sent to  
> one device one after another then all other messages will wait because  
> second message will not be processed till first one is processed.

True, in that case “select” would help. But you can easily write your  
own processor with thread pool. You can get ideas from Queue internals  
or [http://wiki.rubygarden.org/Ruby/page/show/MultiThreading](http://wiki.rubygarden.org/Ruby/page/show/MultiThreading)

Kind regards

```
robert

```
