Skip to content

Instantly share code, notes, and snippets.

@kylekyle
Created April 23, 2015 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylekyle/81679f25fa0ccd74b81e to your computer and use it in GitHub Desktop.
Save kylekyle/81679f25fa0ccd74b81e to your computer and use it in GitHub Desktop.
If you want to search for something in parallel in Ruby, the thread gem is the way to go
# gem install thread
require 'thread/pool'
require 'thread/channel'
require 'ruby-progressbar'
pool = Thread.pool 2
channel = Thread.channel
progress = ProgressBar.create totaL: 1_000_000
1_000_000.times do |i|
pool.process do
# do a bunch of processing here
sleep 1
# send your result here
channel.send i if i == 9
# update progress
progress.increment
end
end
# get your result here
puts "You were looking for the number #{channel.receive}"
# stop processing
pool.shutdown!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment