1. Technology
Send to a Friend via Email
You can opt-out at any time. Please refer to our privacy policy for contact information.

Spotlight on Gems: Threadify


1 of 3

Spotlight on Gems: Threadify

According to the Threadify README, "threadify.rb makes it stupid easy to process a bunch of data using 'n' worker threads." And it really does make it "stupid easy" to process things from an enumerable using threads. It's something everyone should know about, and it might just find its way into your everyday gemset.

Imagine the following situation: You have a list of URLs that you need to download. It's easy to use the each method and be done with it.

urls.each {|u| download(u) }

But this will be quite slow. When something is downloaded, Ruby spends most of its time waiting for the download. You can do this much faster by downloading more than one of these URLs at a time. This is where threadify comes in. The following code will download up to 10 files at a time, and notice that it's just as simple as using each.

urls.threadify(10) {|u| download(u) }
  1. About.com
  2. Technology
  3. Ruby
  4. Gems
  5. Spotlight on Gems: Threadify

©2014 About.com. All rights reserved.