Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > asynchronous select from queue

Reply
Thread Tools

asynchronous select from queue

 
 
Boris Mojo-jojo
Guest
Posts: n/a
 
      12-10-2006
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.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Eric Hodel
Guest
Posts: n/a
 
      12-10-2006
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 Hodel - - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!


 
Reply With Quote
 
 
 
 
Boris Mojo-jojo
Guest
Posts: n/a
 
      12-10-2006
Eric Hodel 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 Hodel - - 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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Vidar Hokstad
Guest
Posts: n/a
 
      12-10-2006

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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      12-11-2006
On 10.12.2006 18:12, Vidar Hokstad wrote:
> 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.


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
 
Reply With Quote
 
Boris Mojo-jojo
Guest
Posts: n/a
 
      12-11-2006
Robert Klemme wrote:
> On 10.12.2006 18:12, Vidar Hokstad 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 ???


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      12-11-2006
On 11.12.2006 12:06, Boris Mojo-jojo wrote:
> 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.


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...MultiThreading

Kind regards

robert
 
Reply With Quote
 
Boris Mojo-jojo
Guest
Posts: n/a
 
      12-11-2006
Robert Klemme 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...MultiThreading
>
> Kind regards
>
> robert


Thanks a lot for the link

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Program blocked in Queue.Queue.get and Queue.Queue.put Kris Python 0 01-04-2012 03:46 PM
why does the following with Queue, q.put('\x02', True) not put itin the queue? Gabriel Rossetti Python 3 04-25-2008 03:41 PM
Is Queue.Queue.queue.clear() thread-safe? Russell Warren Python 4 06-27-2006 03:03 PM
what's the difference between #include "queue.h" and #include "queue.cpp" Kceiw C++ 3 03-14-2006 03:01 AM
Queue.Queue-like class without the busy-wait Paul L. Du Bois Python 29 04-04-2005 01:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57