[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

nuby threading on threads

Peña, Botp

1/24/2005 12:48:00 AM

Hi Friends,

This is my first touch on threads, so be gentle pls :-)

I am trying to execute loop a command by using threads (tested them on
windows and I find them much faster than doing one at a time :).

However, there are some commands that execuite very long time and are memory
intensive, so I want that I only execute 10 threads at a time.

eg.

LIMIT= 10
tlist = []
loop {
if tlist.size< LIMIT
tlist << Thread.new { p "test" }
else
sleep 5
end
}

Am, I right? Of course, I'm wrong since I tested it; tlist size is steady at
LIMIT. When I view tlist, the threads are dead, so how do I shrink tlist
properly? Will the threads just go away if I delete any item in tlist and
compact tlist? Can anyone give some hints pls?

Thanks.

kind regards -botp


4 Answers

Assaph Mehr

1/24/2005 1:15:00 AM

0

Sounds like you need an implementation of thread-pool.
Read http://www.rubygarden.org/ruby?ObjectPoolingAn... - it
might help.

Kaspar Schiess

1/24/2005 9:08:00 AM

0

(In response to news:20050124005127.F2A9982B3@mx2.delmonte-phil.com by
Peña, Botp)

> LIMIT= 10
> tlist = []
> loop {
> if tlist.size< LIMIT
> tlist << Thread.new { p "test" }
> else
> sleep 5
> end
> }
>

Hello,

I'd recommend discarding threads that are dead from the list like this:

tlist.select! { |t| t.alive? }

yours,
kaspar

hand manufactured code - www.tua.ch/ruby



Robert Klemme

1/24/2005 10:14:00 AM

0


"Assaph Mehr" <assaph@gmail.com> schrieb im Newsbeitrag
news:1106529293.644704.189240@c13g2000cwb.googlegroups.com...
> Sounds like you need an implementation of thread-pool.
> Read http://www.rubygarden.org/ruby?ObjectPoolingAn... - it
> might help.

Definitely. A common pattern is this

require 'thread'
PARALLEL = 10
TERMINATOR = Object.new

# setup
threads = []
queue = Queue.new

PARALLEL.times do
threads << Thread.new(queue) do |q|
until ( TERMINATOR == ( obj = q.deq ) )
# do something with obj
puts "#{Thread.current.inspect}: #{obj.inspect}"
# or maybe deal with arbitrary code
begin
obj.call if obj.respond_to? :call
rescue Exception => e
$stderr.puts e
end
end
end
end

# add tasks to queue
20.times {|i| queue.enq "Task 1.#{i}"}
queue.enq lambda { puts "some arbitrary task"; sleep 2 }
queue.enq lambda { puts "another task"; sleep 2 }
queue.enq lambda { raise "Deadly!" }
5.times {|i| queue.enq "Task 2.#{i}"}

# temination
threads.each { queue.enq TERMINATOR }
threads.each { |t| t.join }

# end

Kind regards

robert

Eric Hodel

1/24/2005 5:19:00 PM

0

On 24 Jan 2005, at 01:08, Kaspar Schiess wrote:

> (In response to news:20050124005127.F2A9982B3@mx2.delmonte-phil.com by
> Peña, Botp)
>
>> LIMIT= 10
>> tlist = []

tlist = ThreadGroup.new

>> loop {
>> if tlist.size< LIMIT
>> tlist << Thread.new { p "test" }

tlist.add Thread.new { p "test" }

>> else
>> sleep 5
>> end
>> }

ThreadGroups automatically drop Threads that are no longer alive. Use
tlist.list to get the list of threads currently in the ThreadGroup.

> I'd recommend discarding threads that are dead from the list like this:
>
> tlist.select! { |t| t.alive? }

Threads may die between the time you pick them out and the time you
decide to do something with them. You'll have to check each one in a
critical section before doing some work that requires the thread to be
alive, or rescue an exception around the operation.

--
Eric Hodel - drbrain@segment7.net - http://se...
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04