[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ThreadLimiter 0.1.0

Erik Veenstra

7/13/2008 10:13:00 AM

ThreadLimiter 0.1.0 is released.

RDoc: http://www.erikve...threadlimiter/doc/...
Download: http://rubyforge.org/projects/threadlimiter/...

gegroet,
Erik V. - http://www.erikve...

----------------------------------------------------------------

ThreadLimiter forks threads like Thread.fork(), but limits the
number of concurrently running threads.

ThreadLimiter isn't a thread pool. Each fork really starts a
new thread.

Example: Get the titles of a large collections of URL's.

The traditional way, using Thread directly:

urls = [.....] # A lot of URL's. Maybe
even thousends.

titles =
urls.collect do |url|
Thread.fork do
# ... get the title of the url...
end
end.collect do |thread|
thread.value
end

With ThreadLimiter#fork():

thread_limiter = ThreadLimiter.new(10) # Max. 10 concurrently
running threads.
urls = [.....] # A lot of URL's. Maybe
even thousends.

titles =
urls.collect do |url|
thread_limiter.fork do
# ... get the title of the url...
end
end.collect do |thread|
thread.value
end

With Enumerable#threaded_collect():

urls = [.....] # A lot of URL's. Maybe
even thousends.

titles =
urls.threaded_collect(10) do |url| # Max. 10 concurrently
running threads.
# ... get the title of the url...
end

----------------------------------------------------------------