nuby threading on threads

P

Peña, Botp

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
 
K

Kaspar Schiess

(In response to 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
 
R

Robert Klemme

Assaph Mehr said:
Sounds like you need an implementation of thread-pool.
Read http://www.rubygarden.org/ruby?ObjectPoolingAndThreading - 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
 
E

Eric Hodel

--Apple-Mail-49--569072998
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

(In response to by
Pe=F1a, Botp)
LIMIT=3D 10
tlist =3D []

tlist =3D ThreadGroup.new

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

ThreadGroups automatically drop Threads that are no longer alive. Use=20=

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=20
decide to do something with them. You'll have to check each one in a=20
critical section before doing some work that requires the thread to be=20=

alive, or rescue an exception around the operation.

--=20
Eric Hodel - (e-mail address removed) - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

--Apple-Mail-49--569072998
content-type: application/pgp-signature; x-mac-type=70674453;
name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iD8DBQFB9S4KMypVHHlsnwQRAse8AJ95QijOrWUzbXk4dfAabhSaPL4dYgCghaXm
C03+HHUXm2x0315/5Lpu3r0=
=ZqAn
-----END PGP SIGNATURE-----

--Apple-Mail-49--569072998--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top