Created
April 23, 2015 03:21
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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