1. Computing

Spotlight on Gems: Threadify

By

3 of 3

Using Threadify

Threadify is a simple library with simple goals, which makes it easy to use but there's not much to it. Threadify can only be used in two ways. The first way will take every element of the enumerable in sequence, spawn a thread (up to a maximum number of threads) and pass the element to the block.

The threadify method returns something akin to the map method. It will return an array of what the block returns for each element. Depending on how you're using threadify this may or may not be useful to you.

The following example will visit each URL in the list. If the HTTP server returns 200 OK (the link is "good"), then the block will return the link along with :good in an array. Otherwise it will return the link along with :bad in an array. It will spawn, at most five threads.


#!/usr/bin/env ruby
require 'rubygems'
require 'threadify'
require 'net/http'
require 'uri'

links = %w{
http://ruby.about.com/
http://stackoverflow.com/questions/tagged/ruby
http://www.reddit.com/r/ruby/
http://google.com/rubyisawesome
http://www.ruby-lang.org/en/
http://ruby-doc.org/
http://watir.com/
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29
}

bad_links = links.threadify(5) do|link|
  res = Net::HTTP.get_response( URI.parse(link) )
  
  if res.is_a? Net::HTTPSuccess
    [ link, :good ]
  else
    [ link, :bad ]
  end
end.select{|link| link[1] == :bad }

puts bad_links

However, you may not want to spawn so many threads. Using threadify, you can also specify how many elements from the enumerable to pass to each thread. The following is a minor modification of the previous example. Instead of spawning a set number of threads, it will spawn as many threads as it needs in order to pass five of the links to each threads. In this example, it will only spawn two threads, but if you had 100 links, it would spawn 20 threads.


#!/usr/bin/env ruby
require 'rubygems'
require 'threadify'
require 'net/http'
require 'uri'

links = %w{
http://ruby.about.com/
http://stackoverflow.com/questions/tagged/ruby
http://www.reddit.com/r/ruby/
http://google.com/rubyisawesome
http://www.ruby-lang.org/en/
http://ruby-doc.org/
http://watir.com/
http://en.wikipedia.org/wiki/Ruby_%28programming_language%29
}

bad_links = links.threadify(:each_slice, 5) do|links|
  links.map do|link|
    res = Net::HTTP.get_response( URI.parse(link) )

    if res.is_a? Net::HTTPSuccess
      [ link, :good ]
    else
      [ link, :bad ]
    end
  end
end.flatten(1).select{|link| link[1] == :bad }

puts bad_links
  1. About.com
  2. Computing
  3. Ruby
  4. Gems
  5. Using Threadify

©2014 About.com. All rights reserved.